使用 XDocument 和 XPath 解析 xml
<REETA xmlns="http://pria.org">
<AFFIDAVIT>
<COUNTY_NAME>BOBBIES COUNTY</COUNTY_NAME>
<DOC_TYPE>DEED</DOC_TYPE>
<DOC_DATE>2010-02-19T05:14:58</DOC_DATE>
<GROSS_SELL_PRICE>200000.00</GROSS_SELL_PRICE>
<TAXABLE_SELL_PRICE>200000.00</TAXABLE_SELL_PRICE>
<EXCISE_TAX_STATE>2560.00</EXCISE_TAX_STATE>
<EXCISE_TAX_LOCAL>500.00</EXCISE_TAX_LOCAL>
<DELQ_INT_STATE>0.00</DELQ_INT_STATE>
<DELQ_INT_LOCAL>0.00</DELQ_INT_LOCAL>
<DELQ_PENALTY>0.00</DELQ_PENALTY>
<SUB_TOTAL>3060</SUB_TOTAL>
<STATE_TECH_FEE>5.00</STATE_TECH_FEE>
<PROCESSING_FEE>0.00</PROCESSING_FEE>
<TOTAL_DUE>3065</TOTAL_DUE>
- <INDIVIDUAL type="Buyer">
<NAME>JANE DOE</NAME>
</INDIVIDUAL>
- <INDIVIDUAL type="Seller">
<NAME>JON DOE</NAME>
</INDIVIDUAL>
- <PARCEL>
<NUMBER>3141614</NUMBER>
</PARCEL>
</AFFIDAVIT>
</REETA>
var affidavits = xDocument.Descendants("AFFIDAVIT");
var affidavitsTest = xDocument.XPathEvaluate("/reeta/AFFIDAVIT/COUNTY_NAME");
上面是我从第三方源使用的 xml。由于某种原因,我无法使用上面描述的任何一种方法解析 xml。任何见解都会非常有帮助,非常感谢
<REETA xmlns="http://pria.org">
<AFFIDAVIT>
<COUNTY_NAME>BOBBIES COUNTY</COUNTY_NAME>
<DOC_TYPE>DEED</DOC_TYPE>
<DOC_DATE>2010-02-19T05:14:58</DOC_DATE>
<GROSS_SELL_PRICE>200000.00</GROSS_SELL_PRICE>
<TAXABLE_SELL_PRICE>200000.00</TAXABLE_SELL_PRICE>
<EXCISE_TAX_STATE>2560.00</EXCISE_TAX_STATE>
<EXCISE_TAX_LOCAL>500.00</EXCISE_TAX_LOCAL>
<DELQ_INT_STATE>0.00</DELQ_INT_STATE>
<DELQ_INT_LOCAL>0.00</DELQ_INT_LOCAL>
<DELQ_PENALTY>0.00</DELQ_PENALTY>
<SUB_TOTAL>3060</SUB_TOTAL>
<STATE_TECH_FEE>5.00</STATE_TECH_FEE>
<PROCESSING_FEE>0.00</PROCESSING_FEE>
<TOTAL_DUE>3065</TOTAL_DUE>
- <INDIVIDUAL type="Buyer">
<NAME>JANE DOE</NAME>
</INDIVIDUAL>
- <INDIVIDUAL type="Seller">
<NAME>JON DOE</NAME>
</INDIVIDUAL>
- <PARCEL>
<NUMBER>3141614</NUMBER>
</PARCEL>
</AFFIDAVIT>
</REETA>
var affidavits = xDocument.Descendants("AFFIDAVIT");
var affidavitsTest = xDocument.XPathEvaluate("/reeta/AFFIDAVIT/COUNTY_NAME");
Above is xml which I am consuming from a third party source. For some reason I cannot parse through the xml with either method I describe above. Any insight would be very helpful thank you very much
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该位
不起作用,因为
AFFIDAVIT
位于http://pria.org
命名空间中。这应该可行(虽然尚未测试过):另一种方法是,无需在代码中硬编码名称空间,而是使用根节点的名称空间,如下所示:
xpath 由于区分大小写而不起作用。对于初学者来说,它应该是
REETA
中的内容,而不是reeta
。一旦解决了大小写敏感问题,它也会存在命名空间问题。不过,我不太确定如何在 XPath 中指定名称空间。This bit
doesn't work because the
AFFIDAVIT
is in thehttp://pria.org
namespace. This should work (haven't tested it though):Alternative to that, without having to hardcode the namespace in the code is to use the namespace of the root node like so:
The xpath one doesn't work due to case sensitivity. For starters, it should be
As in
REETA
, notreeta
. It will also have namespace issues once the case sensitivity is resolved. I am not too sure how to specify namespaces in XPath though.您没有指定名称空间。尝试:
You didn't specify the namespace. Try: