I have a very basic XML file that looks like this:
<allData>
<allDataDetails>
<quoteid>ABC123</quoteid>
<customername>John Smith</customername>
</allDataDetails>
<allDataDetails>
<quoteid>DEF456</quoteid>
<customername>Jane Doe</customername>
</allDataDetails>
</allData>
My XSD specifies that at least 1 allDataDetails element must exist. The doc is validated fine.
When querying using Linq to XML, though, I cannot seem to recognize or query for the inner elements within allData. Instead, when I view in the debugger, the Value attribute is all the data concatenated. It looks like this:
ABC123John SmithDEF456Jane Doe
Here’s my query code. myRows is always null, because I cannot seem to get the descendants:
XDocument entityXml = XDocument.Parse(myDataString);
var myRows = from d in entityXml.Descendants("allDataDetails")
select new
{
quoteid = d.Element("quoteid").Value,
customername = d.Element("customername").Value
};
Anyone know what could be wrong here?
I just run your sample code, and few syntax error were fixed:
and also a comma for the value customer name was wrong, try my version below, it should work.