Click here to Skip to main content
15,878,945 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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>

my xpath query is

C#
XmlNode node3 = xdoc.SelectSingleNode("//identifier/text()");


the query suppose to get the first identifier value " BatchProcessed".

but it returns null when i test but when i use the path in xml tool (online) it retruns value.

i don't know what am missing here,some one enlighten me
Posted
Updated 16-Aug-11 18:44pm
v2

You should not use text(). Just get the node, and then get the value of the node in code. XPath implimentations can vary. Using // is also wasteful, if your XML grows, you will find that it's slower than being more explicit in the path you want to search.
 
Share this answer
 
Comments
shan1395 17-Aug-11 0:10am    
even i tried your approach too

XmlNode nlst = xdoc.SelectSingleNode("//identifier");
string value= nlst.Value;

nlst have null value. am not sure what am missing here
Since you want the first identifier's value to be shown, you can do it in this way:

string value = xdoc.SelectSingleNode("//identifier[position()=1]").InnerText; //Returns BatchProcessed


Hope this helps.
 
Share this answer
 
v2
Comments
shan1395 17-Aug-11 0:49am    
this xpath make me crazy,no luck..still i got

Object reference not set to an instance of an object.
Tarun.K.S 17-Aug-11 0:50am    
Did you replace xmlDoc with xdoc? :)
Modified my solution. Check now.
shan1395 17-Aug-11 1:14am    
yeah i changed xdoc
figured it out

xdoc.SelectSingleNode("//*[local-name()='ServiceId']");
 
Share this answer
 
Comments
Tarun.K.S 17-Aug-11 1:26am    
What is a ServiceId?
shan1395 19-Aug-11 11:19am    
xdoc.SelectSingleNode("//*[local-name()='identifier']");

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