I am working with XML that is close to this:
<?xml version="1.0"?>
<ROOT>
<SECTION>
<GROUP1>
<NODE NAME="something" value="some value"/>
<NODE NAME="something" VALUE="some value"/>
</GROUP1>
<GROUP2>
<NODE NAME="something" value="some value"/>
<NODE NAME="something" VALUE="some value"/>
</GROUP2>
</SECTION>
<SECTION>
<GROUP1>
<NODE NAME="something" value="some value"/>
<NODE NAME="something" VALUE="some value"/>
</GROUP1>
<GROUP2>
<NODE NAME="something" value="some value"/>
<NODE NAME="something" VALUE="some value"/>
</GROUP2>
</SECTION>
</ROOT>
I just want to select all the GROUP1 and GROUP2 elements together with a LINQ query. Any help is appreciated.
Try this:
var doc = XDocument.Parse(xmlString);
var groups = doc.Descendants("SECTION").Elements().Where(e => e.Name.LocalName.StartsWith("GROUP"));
Console.Write(groups.Count());
To find which group it comes from, we could get it by the parent of the XElement.
var doc = XDocument.Parse(xmlString);
var group1 = doc.Descendants("GROUP1");
var group2 = doc.Descendants("GROUP2");
is that what you're after?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.