如何获得确切的xpath? [英] How to get the exact xpath?
问题描述
我将以下xmldocument命名为memberDoc:
Hi,
I have the following xmldocument named as memberDoc:
<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>
现在,我需要获取< race>该文件位于[tblmembers/additionalinfo/Patientadditionalinfo [@id =''1'']]]中.
我编写了以下代码:
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;
在这里,我得到的是".
您能帮我什么是正确的XPath或我确切缺少的地方吗?
预先感谢,
Avishek
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
推荐答案
有什么问题
What''s wrong with
string retvalue = memberDoc.SelectSingleNode("ROOT/tblmembers/additionalinfo/patientadditionalinfo[@id='1']/race").InnerText;
?
我将您提供的内容复制并粘贴到名为"test.xml"的文件中(当然,从第二行中删除了多余的破折号)
并测试了此代码:
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 :
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
但是我可以编写XPath,但是我从Pallini的答案中获得了XPath;)
希望对您有所帮助.
However I can write the XPath but I took the XPath from Pallini''s answer ;)
Hope it helps.
这篇关于如何获得确切的xpath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!