将元素添加到XDocument c#

时间:2017-11-03 15:58:45

标签: c# insert linq-to-xml add xelement

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

有人可以告诉我如何将我的初始Xdocument与INSERT合并在一起:

任何帮助表示赞赏!谢谢

INITIAL 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:

       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"))));

1 个答案:

答案 0 :(得分:0)

如果您只想附加到名为“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);