使用 XDocument 和 XPath 解析 xml

发布于 2024-08-23 06:48:39 字数 1432 浏览 4 评论 0原文

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

反差帅 2024-08-30 06:48:39

该位

var affidavits = xDocument.Descendants("AFFIDAVIT");

不起作用,因为 AFFIDAVIT 位于 http://pria.org 命名空间中。这应该可行(虽然尚未测试过):

var affidavits = xDocument.Descendants("{http://pria.org}AFFIDAVIT");

另一种方法是,无需在代码中硬编码名称空间,而是使用根节点的名称空间,如下所示:

var affidavits = xDocument.Descendants(xDocument.Root.Name.Namespace + "AFFIDAVIT");

xpath 由于区分大小写而不起作用。对于初学者来说,它应该是

var affidavitsTest = xDocument.XPathEvaluate("/REETA/AFFIDAVIT/COUNTY_NAME");

REETA 中的内容,而不是 reeta。一旦解决了大小写敏感问题,它也会存在命名空间问题。不过,我不太确定如何在 XPath 中指定名称空间。

This bit

var affidavits = xDocument.Descendants("AFFIDAVIT");

doesn't work because the AFFIDAVIT is in the http://pria.org namespace. This should work (haven't tested it though):

var affidavits = xDocument.Descendants("{http://pria.org}AFFIDAVIT");

Alternative to that, without having to hardcode the namespace in the code is to use the namespace of the root node like so:

var affidavits = xDocument.Descendants(xDocument.Root.Name.Namespace + "AFFIDAVIT");

The xpath one doesn't work due to case sensitivity. For starters, it should be

var affidavitsTest = xDocument.XPathEvaluate("/REETA/AFFIDAVIT/COUNTY_NAME");

As in REETA, not reeta. It will also have namespace issues once the case sensitivity is resolved. I am not too sure how to specify namespaces in XPath though.

看透却不说透 2024-08-30 06:48:39

您没有指定名称空间。尝试:

XNamespace ns = "http://pria.org";
var affidavits = xDocument.Descendants(ns + "AFFIDAVIT");

You didn't specify the namespace. Try:

XNamespace ns = "http://pria.org";
var affidavits = xDocument.Descendants(ns + "AFFIDAVIT");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文