本文是小编为大家收集整理的关于如何获得准确的xpath?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。
问题描述
你好,
我有以下名为 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 ;)
希望对您有所帮助.
问题描述
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>
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
推荐答案
What''s wrong with
string retvalue = memberDoc.SelectSingleNode("ROOT/tblmembers/additionalinfo/patientadditionalinfo[@id='1']/race").InnerText;
?
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
However I can write the XPath but I took the XPath from Pallini''s answer ;)
Hope it helps.
相关标签/搜索
unity 怎么给矩阵赋值
一个机器人只能向下和向右移动,
oracle关闭数据库后打不开
hibernate增删查改sql怎么写
c# USB 设备枚举
api-ms-win 是什么文件
编译原理 LR(0) 项目集簇 DFA 直接构造
sharePoint无法lookup选项属性的值
centos和denied有什么区别
.net core 分布式 并发 锁
app abi 怎么勾选
ios uitextview xib 高度
c++
linux
performance
编程日记
博客专题系列
selenium
ide
xml
firefox
xpath