开发者

Parsing xml with XDocument and XPath

开发者 https://www.devze.com 2022-12-21 09:55 出处:网络
<REETA xmlns=\"http://pria.org\"> <AFFIDAVIT> <COUNTY_NAME>BOBBIES COUNTY</COUNTY_NAME>
<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<开发者_StackOverflow社区;/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


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.


You didn't specify the namespace. Try:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号