Parsing xml with XDocument and XPath

16,049

Solution 1

You didn't specify the namespace. Try:

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

Solution 2

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.

Share:
16,049
Admin
Author by

Admin

Updated on June 14, 2022

Comments

  • Admin
    Admin almost 2 years
    <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

  • Kugel
    Kugel almost 13 years
    I would use ns.GetName() instead of string concatenation
  • Kevin Brock
    Kevin Brock almost 12 years
    @Kugel - Actually the suggestion on MSDN is to use the string concatentation as the operator is overridden for that purpose: "For C#, the recommended approach for creating an XName in a namespace is to declare the XNamespace object, then use the override of the addition operator." Found in the "Creating an XName in a Namespace" section: msdn.microsoft.com/en-us/library/system.xml.linq.xname