Possible Duplicate:
Parse XML Elements with LINQ
I have an XML document that looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<TestDataset xmlns="http://tempuri.org/TestDataset.xsd">
<TaxRate>
<Code>GST</Code>
<Rate>0.05</Rate>
<LastModified>2010-03-31T17:03:24.063-04:00</LastModified>
<Deleted>0</Deleted>
<AbbreviationEN>GST</AbbreviationEN>
<AbbreviationFR>GST</AbbreviationFR>
<GLSubCode>GST</GLSubCode>
</TaxRate>
<TaxRate>
<Code>PST</Code>
<Rate>0.08</Rate>
<LastModified>2010-03-31T17:03:24.063-04:00</LastModified>
<Deleted>0</Deleted>
<AbbreviationEN>PST</AbbreviationEN>
<AbbreviationFR>PST</AbbreviationFR>
<GLSubCode>PST</GLSubCode>
</TaxRate>
</TestDataset>
And I am trying to select it’s contents with the following query:
XDocument data = LoadTestData("TaxRate.xml");
var taxdata = (from x in data.Elements("TaxRate")
select new
{
Code = x.Element("Code").Value,
Rate = x.Element("Rate").Value,
AbbreviationEN = x.Element("AbbreviationEN").Value,
AbbreviationFR = x.Element("AbbreviationFR").Value,
GLSubCode = x.Element("GLSubCode").Value
}).ToList();
However I keep getting no results, LoadTestData is loading the XML document fine.
You forgot to include the namespace:
Although the namespace in your XML file is the default namespace, you still need to include it in your query.