4
<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

2
  • 1
    I have tried chrissr suggestion and Igor zevaka suggestions. Igor and chrissr pointed out I need to include the namespace. Upon including the namespace, I am unable to parse the xml. What happens is xdocument.descenants("{pria.org}AFFIDAVIT"); returns the entire xml, and the xpathevaluate returns enumeration not yeilded Commented Feb 22, 2010 at 22:44
  • It worked, thank you. I typed in the url in the ns incorrect. thank you to both chrissr and igor Commented Feb 22, 2010 at 22:46

2 Answers 2

9

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.

Sign up to request clarification or add additional context in comments.

Comments

8

You didn't specify the namespace. Try:

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

2 Comments

I would use ns.GetName() instead of string concatenation
@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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.