将元素添加到 XDocument c#

Adding Elements to an XDocument c#

您好,我很难找到将 2 XML 代码合并在一起的解决方案。

有人可以告诉我如何将我的初始 Xdocument 与下面的 INSERT 合并:

感谢任何帮助!谢谢

初始 XDOC:

        XDocument doc = new XDocument(
            new XDeclaration("1.0", "utf-8", "true"),
            new XElement(ns + "Root",
                new XAttribute("xmlns", "http://www.blabla.com"),
                new XElement(ns + "LEVEL1", "A"),
                new XElement(ns + "LEVEL1", "B"),
                new XElement(ns + "LEVEL1",
                    new XElement(ns + "LEVEL2", "A"),
                    new XElement(ns + "LEVEL2", "B"),
                    new XElement(ns + "LEVEL2", "C"),
                    new XElement(ns + "LEVEL2")),

                        <Insert Here>

                new XElement(ns + "LEVEL1", "D")));

插入:

       INSERT= new XElement(ns + "LEVEL3",
                   new XElement(ns + "LEVEL4", "A"),
                   new XElement(ns + "LEVEL4", "B"),
                   new XElement(ns + "LEVEL4", "C"),
                   new XElement(ns + "LEVEL4",
                       new XElement(ns + "LEVEL5", "A"),
                       new XElement(ns + "LEVEL5", "B"),
                       new XElement(ns + "LEVEL5", "C"),
                       new XElement(ns + "LEVEL5", "D")),
                   new XElement(ns + "LEVEL4", "E"));

期望的输出:

 XDocument doc = new XDocument(
        new XDeclaration("1.0", "utf-8", "true"),
        new XElement(ns + "Root",
            new XAttribute("xmlns", "http://www.blabla.com"),
            new XElement(ns + "LEVEL1", "A"),
            new XElement(ns + "LEVEL1", "B"),
            new XElement(ns + "LEVEL1",
                new XElement(ns + "LEVEL2", "A"),
                new XElement(ns + "LEVEL2", "B"),
                new XElement(ns + "LEVEL2", "C"),
                new XElement(ns + "LEVEL2"
                    new XElement(ns + "LEVEL3",
                        new XElement(ns + "LEVEL4", "A"),
                        new XElement(ns + "LEVEL4", "B"),
                        new XElement(ns + "LEVEL4", "C"),
                        new XElement(ns + "LEVEL4",
                            new XElement(ns + "LEVEL5", "A"),
                            new XElement(ns + "LEVEL5", "B"),
                            new XElement(ns + "LEVEL5", "C"),
                            new XElement(ns + "LEVEL5", "D")),
                        new XElement(ns + "LEVEL4", "E"))
            new XElement(ns + "LEVEL1", "D"))));

如果您只想附加到名为 "LEVEL2" 的元素的最后一个实例,您可以使用代码:

XNamespace ns = "http://www.w3.org/2001/XMLSchema-instance";
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "true"),
new XElement(ns + "Root",
    new XAttribute("xmlns", ns),
    new XElement(ns + "LEVEL1", "A"),
    new XElement(ns + "LEVEL1", "B"),
    new XElement(ns + "LEVEL1",
        new XElement(ns + "LEVEL2", "A"),
        new XElement(ns + "LEVEL2", "B"),
        new XElement(ns + "LEVEL2", "C"),
        new XElement(ns + "LEVEL2")),                    

    new XElement(ns + "LEVEL1", "D")));

XElement childElement = new XElement(ns + "LEVEL3",
new XElement(ns + "LEVEL4", "A"),
new XElement(ns + "LEVEL4", "B"),
new XElement(ns + "LEVEL4", "C"),
new XElement(ns + "LEVEL4",
    new XElement(ns + "LEVEL5", "A"),
    new XElement(ns + "LEVEL5", "B"),
    new XElement(ns + "LEVEL5", "C"),
    new XElement(ns + "LEVEL5", "D")),
new XElement(ns + "LEVEL4", "E"));

XElement level2Node = doc.Descendants(ns+"LEVEL2").Last();
level2Node.Add(childElement);