Solved

Need help with XPath

Posted on 2012-03-12
4
332 Views
Last Modified: 2012-03-12
I have an xml file with the following content
<Root>   
<Node>     
<SubEl1>abc</SubEl1>     
<SubEl2>def</SubEl2>     
<SubEl3>123</SubEl3>     
<SubEl4>456</SubEl4>         
</Node> 
<Node>     
<SubEl1>abc</SubEl1>     
<SubEl2>fgi</SubEl2>     
<SubEl3>124</SubEl3>     
<SubEl4>579</SubEl4>         
</Node> 
<Node>     
<SubEl1>ghi</SubEl1>     
<SubEl2>klm</SubEl2>     
<SubEl3>789</SubEl3>     
<SubEl4>012</SubEl4>         
</Node> 
</Root> 

Open in new window


I need to retrieve all the Node elements where SubEl1 has a value of "abc". Actually what I really need is a list of SubEl2 values for those Node elements.

I am having trouble writing XPath expression. Here is what I got so far:

XmlNodeList myList = xdoc.SelectNodes("Root/Node/SubEl1[text()='abc']");

Open in new window


but the above only give me a list of SubEl1, i.e. "abc", "abc",. while what I need is SubEl2, i.e. "def", "fgi"
0
Comment
Question by:YZlat
  • 3
4 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 250 total points
Comment Utility
XmlNodeList myList = xdoc.SelectNodes("Root/Node[SubEl1='abc']/SubEl2");
0
 
LVL 4

Expert Comment

by:MichaelStaszewski
Comment Utility
You almost have it. Your expression should be...

Root/Node/SubEl1[text()='abc']/../SubEl2

Open in new window

0
 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
Michaels solution will work but is highly inefficient.
Why go down and back up if you don't need to?

Also (I should have made that comment earlier) it is never a good idea to test explicitely for a text() node. XPath can split a text content in multiple text() nodes, and then this would not work
I you want to drill down to the text level, you should do
SubEl1[.='abc']
or even better
SubEl1[normalize-space(.) ='abc']
0
 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
You can't delete a question that provided you with a good solution
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now