BOBBIES COUNTY DEED...">

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</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 the xml that I am consuming from a third-party source. For some reason, I cannot parse the XML using any of the methods described above. Any insight would be very helpful, thank you very much

+4
source share
2 answers

You did not specify a namespace. Try:

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

This bit

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

does not work because AFFIDAVIT is in the http://pria.org namespace. This should work (not tested it):

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

An alternative to this, without having to hardcode the namespace in the code, is to use the node root namespace as follows:

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

xpath does not 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 problems after eliminating case sensitivity. I'm not too sure how to specify namespaces in XPath though.

+9
source

Source: https://habr.com/ru/post/1302082/


All Articles