如何获得准确的xpath?[英] How to get the exact xpath?

问题描述

你好,
我有以下名为 memberDoc 的 xmldocument:

<ROOT>- <tblmembers><facilityid>A</facilityid><memberid>000000001</memberid><附加信息><adtsource>HIS</adtsource><耐心附加信息 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>休斯顿</patempcity><patempstate>TX</patempstate><patempzipcode>77030</patempzipcode><patempcountry>美国</patempcountry><patempstartdate>20100101</patempstartdate><patempretirementdate></patempretirementdate><address3>ADDRESS3</address3><oldmemberid></oldmemberid><mname></mname><hipaaonfile></hipaaonfile><hipaadate>12/14/2010</hipaadate><race>C</race><语言></language><宗教>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'']] 里面.
我写了以下代码:
XmlNodeList member_nodes;                
 member_nodes = memberDoc.SelectNodes("//tblmembers");

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


在这里,我得到的是"".
你能帮我什么应该是正确的 XPath 或者我到底在哪里失踪?

提前谢谢,
Avishek

推荐答案

出了什么问题
string retvalue = memberDoc.SelectSingleNode("ROOT/tblmembers/additionalinfo/patientadditionalinfo[@id='1']/race").InnerText;

?

我将您提供的内容复制并粘贴到名为"test.xml"的文件中(当然从第二行删除了多余的破折号)

测试了这段代码:
XmlDocument rawXMLData = new XmlDocument();StreamReader reader = new StreamReader(@"d:\test.xml");rawXMLData.LoadXml(reader.ReadToEnd());var result = rawXMLData.SelectNodes("ROOT/tblmembers/附加信息/患者附加信息[@id='1']/race");foreach (XmlNode 节点 in 结果)Console.WriteLine(node.Name + " = " + node.InnerText);Console.ReadLine();


它打印:
race = C

但是我可以编写 XPath,但我从 Pallini 的回答中获取了 XPath ;)

希望对您有所帮助.

本文地址:https://www.itbaoku.cn/post/1394931.html