Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi,
I have the following xmldocument named as memberDoc:
XML
<ROOT>
  - <tblmembers>
    <facilityid>A</facilityid>
    <memberid>000000001</memberid>
    <additionalinfo>
      <adtsource>HIS</adtsource>
      <patientadditionalinfo id="1">
        <patempid>TMH</patempid>
        <patempname>THE METHODIST HOSPITAL</patempname>
        <patempstatus>F</patempstatus>
        <patempoccupation>IT</patempoccupation>
        <patempphone></patempphone>
        <patempaddress>6565 FANNIN</patempaddress>
        <patempaddress2></patempaddress2>
        <patempcity>HOUSTON</patempcity>
        <patempstate>TX</patempstate>
        <patempzipcode>77030</patempzipcode>
        <patempcountry>USA</patempcountry>
        <patempstartdate>20100101</patempstartdate>
        <patempretirementdate></patempretirementdate>
        <address3>ADDRESS3</address3>
        <oldmemberid></oldmemberid>
        <mname></mname>
        <hipaaonfile></hipaaonfile>
        <hipaadate>12/14/2010</hipaadate>
        <race>C</race>
        <language></language>
        <religion>ANG</religion>
        <primcarephysicianid>002249</primcarephysicianid>
        <primcarephysicianlname>CAGE</primcarephysicianlname>
        <primcarephysicianfname>JAMES</primcarephysicianfname>
        <primcarephysicianmname>WILLIAM</primcarephysicianmname>
        <balance></balance>
      </patientadditionalinfo>
    </additionalinfo>
  </tblmembers>
</ROOT>


Now I need to fetch the <race> which is inside [tblmembers/additionalinfo/patientadditionalinfo[@id='1']].
I have written the following code:
XmlNodeList member_nodes;                
 member_nodes = memberDoc.SelectNodes("//tblmembers");

string retvalue=member_nodes[0].SelectSingleNode("//additionalinfo/patientadditionalinfo[@id='1']/race").InnerText;


Here I am getting the as "".
Can you please help me what should be the correct XPath or where exactly I am missing?

Thanks in advance,
Avishek
Posted
Updated 1-Feb-12 2:20am
v2

What's wrong with
C#
string retvalue = memberDoc.SelectSingleNode("ROOT/tblmembers/additionalinfo/patientadditionalinfo[@id='1']/race").InnerText;

?
 
Share this answer
 
Comments
avishekrc 1-Feb-12 9:13am    
Upto memberDoc.SelectSingleNode("ROOT/tblmembers/additionalinfo").InnerText is working fine.
Whenever I am giving memberDoc.SelectSingleNode("ROOT/tblmembers/additionalinfo/patientadditionalinfo[@id='1']").InnerText...it is coming as null.
CPallini 1-Feb-12 9:21am    
Strange enough, I got correctly "C".
Amir Mahfoozi 1-Feb-12 10:05am    
+5 It works CPallini :)
I copied and pasted what you have provided into a file named "test.xml" (of course removed that extra dash from second line)

and tested this code :
C#
XmlDocument rawXMLData = new XmlDocument();
StreamReader reader = new StreamReader(@"d:\test.xml");

rawXMLData.LoadXml(reader.ReadToEnd());

var result = rawXMLData.SelectNodes("ROOT/tblmembers/additionalinfo/patientadditionalinfo[@id='1']/race");

foreach (XmlNode node in result)
   Console.WriteLine(node.Name + " = " + node.InnerText);

Console.ReadLine();


And it prints :
race = C

However I can write the XPath but I took the XPath from Pallini's answer ;)

Hope it helps.
 
Share this answer
 

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