开发者

Select more then one node from XML using LINQ

开发者 https://www.devze.com 2022-12-30 04:08 出处:网络
I have such XML <root> <content> .... </content> <index> .... </index> <keywords>

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?


var elements = 
    from element in xml.Root.Elements()
    where element.Name == "content" ||
          element.Name == "index"
    select element;
var newContentNode = new XElement("content", elements);


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.


Using lambda:

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

精彩评论

暂无评论...
验证码 换一张
取 消