Click here to Skip to main content
15,905,419 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I have below XML

XML
<Header>
  <reference>
    <identifier>BatchProcessed</identifier>
    <datetime>2011-08-15T05:12:00+10:00</datetime>
  </reference>
</Header>
<Header>
  <reference>
    <identifier>Running</identifier>
    <datetime>2011-08-15T05:12:00+10:00</datetime>
  </reference>
</Header>


Am trying to xpath query to find out this value "BatchProcessed" in Indentifier.

Please correct me ,this what i have in my query but its not working

//Header[@Identifier='BatchProcessed']<br />

Thanks in Advance
Posted
Updated 15-Aug-11 18:39pm
v3

Your XPath was almost correct. You'll just have to leave out the @ sign. The @ sign is used to indicate that you are trying to check an attribute. To check for element content all you need to to is omit the @:

XML
//Header/reference[Identifier = "BatchProcessed"]


I'm not sure if the double quotes are really needed, but I put them there just the same as I usually write my XPaths that way. Your XPath will fetch you all Header tags that have child elements reference with sub children identifier with a text value of "BatchProcessed".

Best Regards,

—MRB
 
Share this answer
 
Comments
shan1395 16-Aug-11 2:06am    
some how my count always "0" am not sure why
int count = oDoc.SelectNodes("//Header[.//Identifier='BatchProcessed']").Count;
some one guide me on the right way.
Please refer this link

http://msdn.microsoft.com/en-us/library/ms256086.aspx[^]

OR

You XML format doesn't have root element. Please include the root element

HTML
<root>
   <header>
      <reference>
        <identifier>BatchProcessed</identifier>
        <datetime>2011-08-15T05:12:00+10:00</datetime>
      </reference>
    </header>
    <header>
      <reference>
        <identifier>Running</identifier>
        <datetime>2011-08-15T05:12:00+10:00</datetime>
      </reference>
    </header>
</root>


and use can use C# code to find the particular Node.

C#
XmlDocument oDoc = new XmlDocument();
oDoc.LoadXml(sXML);
XmlNodeList list = oDoc.SelectNodes("//root/Header/reference[identifier = 'BatchProcessed']");
 
Share this answer
 
v2
Comments
shan1395 16-Aug-11 2:06am    
i tried something different way ,some how my count always "0" am not sure why
int count = oDoc.SelectNodes("//Header[.//Identifier='BatchProcessed']").Count;
some one guide me on the right way.
senthil sennu 16-Aug-11 2:23am    
int count = oDoc.SelectNodes("//root/Header/reference[identifier = 'BatchProcessed']").Count;

Use 'identifier' not 'Identifier', because XML is case sensitive.
The above Xpath is working fine. I check running in my machine. Hope this helps.

Senthil S
shan1395 16-Aug-11 3:00am    
Thanks Senthil

finally i used this query to solve the problem

//Identifier[text() = \"BatchProcessed\"]
senthil sennu 16-Aug-11 3:05am    
You are welcome. But the above said solution also work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900