开发者

Parsing xml with XDocument and XPath

<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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜