【问题标题】:Wrapping Children of an XML Document [closed]包装 XML 文档的子项 [关闭]
【发布时间】:2014-06-12 19:24:38
【问题描述】:

我将如何包装我的根的所有子元素? 我目前拥有的示例:

<?xml version="1.0" encoding="utf-16"?>
<root>
    <element>YUP</element>
    <element>YUP</element>
    <element>YUP</element>
</root>

但我需要它来添加一个封装所有这些根的孩子的新孩子,如下所示:

<?xml version="1.0" encoding="utf-16"?>
<root>
    <newsubroot>
         <element>YUP</element>
         <element>YUP</element>
         <element>YUP</element>
    </newsubroot>
</root>

我没有首选(或强制)技术 - 欢迎使用 XmlDocumentXDocument 的任何示例。

【问题讨论】:

  • 阅读一些您必须阅读的文章,在阅读少量文档时解决方案就是!使用你的力量。
  • 哈哈,你能指出我正确的方向吗,然后我尝试查看一些示例并阅读 MSDN,但似乎没有什么是完全正确的。
  • 不,您的选择使用 XDocument 或 XmlDocument。听起来你要求我们为你做这件事。请展示您的尝试。
  • @Yoda 在你自己的技能中,你不依赖。在 Light side XDocument 类中,LINQ-to-XML 和XElement 构造函数将是解决方案。遵循这条路径:stackoverflow.com/questions/4562571/…
  • @pasty 感谢您编辑问题并使其更准确。

标签: c# xml


【解决方案1】:
XDocument xDoc = XDocument.Parse(
    @"<?xml version=""1.0"" encoding=""utf-16""?>
    <root>
        <element>YUP</element>
        <element>YUP</element>
        <element>YUP</element>
    </root>");

var newsubroot = new XElement("newsubroot");
newsubroot.Add(xDoc.Root.Elements());
xDoc.Root.RemoveAll();
xDoc.Root.Add(newsubroot);

【讨论】:

    【解决方案2】:

    使用LINQ2XML 的一种可能解决方案如下所示:

    • 获取元素
    • 复制它们(因为它们是通过引用获取的)
    • 删除它们
    • 在根中添加新的子节点
    • 将复制的元素插入新节点

    代码如下所示:

        var xml = @"<?xml version=""1.0"" encoding=""utf-16""?>
    <root>
        <element>YUP</element>
        <element>YUP</element>
        <element>YUP</element>
    </root>";
    
    try
    {
        var document = XDocument.Parse(xml);
        var root = document.Root;
        // get all "element" elements
        var yups = root.Descendants("element");
        // get a copy of the nodes that are to be moved inside new node
        var copy = yups.ToList();
        // remove the nodes from the root
        yups.Remove();
        // put them in the new sub node
        root.Add(new XElement("newSubRoot", copy));
        // output or save
        Console.WriteLine(document.ToString()); // document.Save("c:\\xml.xml");
    }
    catch (Exception exception)
    {
        Console.WriteLine(exception.Message);
    }
    

    输出是:

    <root>
        <newSubRoot>
            <element>YUP</element>
            <element>YUP</element>
            <element>YUP</element>
        </newSubRoot>
    </root>
    

    【讨论】:

      【解决方案3】:

      你可以像下面这样使用 XDocument

      XDocument doc = XDocument.Parse(xml);
      foreach (XElement subroot in doc.Descendants("newsubroot"))
      {
          // get subroot elements
          foreach (var subsubroot in subroot.Descendants("element"))
          {
              // get subsubroot elements
          }
      }
      

      看这个例子:Query an XDocument for elements by name at any depth

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-03
        • 1970-01-01
        • 2011-03-12
        • 2014-03-20
        • 2010-11-24
        相关资源
        最近更新 更多