简体   繁体   中英

Adding descendents to xdocument root element issue

I have a set of descendent nodes I want to add from one xml into the root element of another xml, but in doing this, rather than getting a structured xml file, when I open the document, I get a big lump of long lines with the descendents of the original document.

Is there a way to add the descendent nodes of one document into the root element of another document?

My code is like so:

foreach (var v in doc.Descendants())
{
    if (v.Name.LocalName == "NOSDocument")
    {
        doc2.Root.Add(doc.DescendantNodes());
    }
}

If you are trying to copy all the nodes from one document to another you can do it this way:

XElement doc1 = XElement.Load("doc1_file.xml");
XElement doc2 = new XElement("doc2");

foreach(XElement child in doc1.Elements())
    doc2.Add(child);

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