Managing XPaths

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I deal with a lot of XML data in my job. One constant requirement is to be able to manage XPath. I may have an XML Document and need a particular node set in that document, then I might do a XPathSelectElements and need to acces the same
node set, but my XPath has changed. I may need to pass an XML Document from one namespace to another and need a common place to store (and share) the location of particular node sets within an XML Document.
One option Ive tried is to create a static Global class to store my XPaths in. But, if I have an XML document which I now have to iterate on using XPathSelectElements, then my XPath is no longer correct.
For example, if my XML document looks like
<pre class="prettyprint <root>
<sub1>
<sub2></sub2>
<sub2></sub2>
</sub1>
<sub1></sub1>
<sub1></sub1>
<sub1></sub1>
</root>[/code]
<br/>
I can set a static Property in a static class "Global" called "xpSub2_a" to the value @"/root/sub1/sub2". If, instead, I do

<pre class="prettyprint List<XElement> sub1set = xdoc.XPathSelectElements(@"/root/sub1");
foreach (XElement item in sub1set)
{
List<XElement> sub2sets = item.XPathSelectElements(Global.xpSub2_b);
}[/code]
Global.xpSub2_b would be equal to "sub2".
So, I end up with something that looks like
<pre class="prettyprint static Global
{
public string const xpSub2_b = "sub2";
public string const xpSub2_a = @"/root/sub1/sub2";
}[/code]
<br/>
Is there a cleaner way to manage all of these XPath statements?
<br/>
<br/>
<br/>

View the full article
 
Back
Top