Reputation: 11
I have an XML document that looks like this. It has been created using the Diagram feature of DevExpress.
<XtraSerializer version="17.1.3.0">
<Items>
<Item1 ItemKind="DiagramRoot">
<Children>
<Item1 Shape="Triangle" Content="AA" />
<Item2 Shape="Triangle" Content="AB" />
<Item3 Shape="Octagon" Content="A" />
</Children>
</Item1>
</Items>
</XtraSerializer>
I would like to Query it to return the Shape and Content of all items under Children. I tried the query below but it does not work.
XDocument document = XDocument.Load("C:\\Users\\Jb\\Desktop\\Network_Base.xml");
var items = from r in document.Descendants("Children")
select new
{
Content = r.Attribute("Content").Value,
Shape = r.Attribute("Shape").Value,
};
foreach (var r in items)
{
Console.WriteLine(r.Content + r.Shape);
}
Upvotes: 0
Views: 78
Reputation: 34421
Try following :
var results = doc.Descendants("Children").FirstOrDefault().Elements()
.Where(x => x.Attribute("Content") != null).Select(r => new {
Content = r.Attribute("Content").Value,
Shape = r.Attribute("Shape").Value
}).ToList();
Upvotes: 2