Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

c# - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...