4

I have such XML

<root>
    <content>
        ....
    </content>
    <index>
        ....
    </index>
    <keywords>
        ....
    </keywords>
</root>

But I need to select just and nodes.

<content>
    ....
</content>
<index>
    ....
</index>

I found out how to select just one node.

XElement Content = new XElement("content", from el in xml.Elements() select el.Element("content").Elements());

How can I get both nodes?

1

3 Answers 3

6
var elements = 
    from element in xml.Root.Elements()
    where element.Name == "content" ||
          element.Name == "index"
    select element;
var newContentNode = new XElement("content", elements);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! It is precisely what I was looking for! :)
1

Once you have the xml file loaded, you can get all the content nodes through:

var cons = from con in xmlFile.Descendants("content");

and similarly you can get the index nodes as:

var idxs = from idx in xmlFile.Descendants("index")

I don't think you can query two nodes using one query string.

Comments

1

Using lambda:

    var elements = document
        .Descendants()
        .Where(element => element.Name == "content" || element.Name == "index");

Comments

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.