0

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.

1
  • I do need to be able to reference which group the node came out of later. Commented Nov 14, 2014 at 1:58

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0
var doc = XDocument.Parse(xmlString);

var group1 = doc.Descendants("GROUP1");
var group2 = doc.Descendants("GROUP2");

is that what you're after?

1 Comment

Close but I want to avoid the two queries because the data can be treated the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.