Reputation: 91
I have this XML File and want write all values with MGU tags that are under the < Norm > with attribute Name="TL 52146" to a list:
<?xml version="1.0" encoding="utf-8"?>
<Normen>
<Norm Name="TL 52146">
<MGU>PV 1401</MGU>
<MGU>PV 1425</MGU>
<MGU>PV 1448</MGU>
</Norm>
</Normen>
The expected result would be:
When I use this code I just get one list element with all MGUs in it but I want every MGU to be a seperate entry in my list:
XDocument doc = XDocument.Load("data/data.xml");
var ChildsOfNorm = from element in doc.Descendants("Norm")
where element.Attribute("Name").Value == "TL 52146"
select element;
Can someone please help me?
Upvotes: 1
Views: 177
Reputation: 1438
I'd change the code to the following:
var ChildsOfNorm = doc
.Descendants("Norm")
.Where(e => e.Attribute("Name").Value == "TL 52146")
.Elements();
If you only want to get the MGU
elements, change the Elements()
call to Elements("MGU")
.
This should get all wanted elements.
With the given xml
foreach (var v in ChildsOfNorm)
Console.WriteLine(v.Value);
outputs:
PV 1401
PV 1425
PV 1448
Upvotes: 2