简体   繁体   中英

LINQ to XML Newbie: Moving Nodes From One Node To Another

Greetings!

I have an XElement object that contains the following:

<Root>
    <SubSections>
        <SubSection id="A">
            <Foo id="1">
                <Bar />
                <Bar />
                <Bar />
            </Foo>
            <Foo id="2">
                <Bar />
                <Bar />
            </Foo>
            <Foo id="3">
                <Bar />
            </Foo>
        </SubSection>
        <SubSection id="B">
            <Foo id="4">
                <Bar />
                <Bar />
                <Bar />
            </Foo>
            <Foo id="5">
                <Bar />
                <Bar />
            </Foo>
         </SubSection>
        <SubSection id="C">

        </SubSection>
    </SubSections>
</Root>

I'd like to move Foo's 2 and 3 to the SubSection with the id of "C" such that the result is:

<Root>
    <SubSections>
        <SubSection id="A">
            <Foo id="1">
                <Bar />
                <Bar />
                <Bar />
            </Foo>
        </SubSection>
        <SubSection id="B">
            <Foo id="4">
                <Bar />
                <Bar />
                <Bar />
            </Foo>
            <Foo id="5">
                <Bar />
                <Bar />
            </Foo>
        </SubSection>
        <SubSection id="C">
            <Foo id="2">
                <Bar />
                <Bar />
            </Foo>
            <Foo id="3">
                <Bar />
            </Foo>
        </SubSection>
    </SubSections>
</Root>

What's the best way to go about moving Foo sections "2" and "3" to the "C" SubSection?

You need to get Foo sections 2 and 3 with a query like:

var foos = from xelem in root.Descendants("Foo")
           where xelem.Attribute("id").Value == "2" || xelem.Attribute("id").Value == "3"
           select xelem;

And then iterate that list and remove them from their parents with

xelem.Remove();

Then just add them to the correct node with:

parentElem.Add(xelem);

The first query will get you both sections then remove and add each one to the correct place on the tree.

Here's a complete solution:

var foos = (from xElem in xDoc.Root.Descendants("Foo")
                   where xElem.Attribute("id").Value == "2" || xElem.Attribute("id").Value == "3"
                   select xElem).ToList();

        var newParentElem = (from xElem in xDoc.Root.Descendants("SubSection")
                            where xElem.Attribute("id").Value == "C"
                            select xElem).Single();

        foreach(var xElem in foos)
        {
            xElem.Remove();
            newParentElem.Add(xElem);
        }

After that your xDoc should have the correct tree.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM