I'm just learning Linq, and stuck on what I hope is fairly simple. My xml document is like:
<?xml version="1.0" encoding="utf-8"?>
<XDOC>
...
<ItemsDetail>
<Item name="Item1">
<data1>
<Data type="classA">55</Data>
<Data type="classB">66</Data>
</data1>
<data2>
<Data type="classA">77</Data>
<Data type="classB">88</Data>
</data2>
</Item>
</ItemsDetail>
</XDOC>
So I load my XML above into an XDocument
type and then query the
var query = from p in ILSXml.Elements("XDOC").Elements("ItemsDetail").Elements("Item")
select p;
Then I run a foreach
on on the query.
foreach (var record in query)
{
Console.WriteLine("Name: {0}", record.Attribute("Name").Value);
Console.WriteLine("Data1 ClassA: {0}", record.Element("data1").Element("Data").Attribute("classA").Value);
}
So the line:
Console.WriteLine("Data1 ClassA: {0}", record.Element("data1").Element("Data").Attribute("classA").Value);
does not work which, is what I was pretty much expecting. Do I have to run another series of queries or run some inline anonymous methods?
Oh and please don't comment on the xml, it's not mine, I just have to work with it.