如何将节点放入父节点?

How to put nodes into parent node?

我想添加一个具有属性的父节点,并将节点放入该父节点内。我有以下 xml:

<?xml version="1.0" encoding="UTF-8"?>
<foo>
   <row>
      <PHONE_NUM>124</PHONE_NUM>
      <PHONE_ORDER>1</PHONE_ORDER>      
   </row>
   <row>
      <PHONE_NUM>123</PHONE_NUM>
      <PHONE_ORDER>2</PHONE_ORDER>      
   </row>
   <row>
      <PHONE_NUM>181</PHONE_NUM>
      <PHONE_ORDER>3</PHONE_ORDER>      
   </row>   
</foo>

我试过这种方法:

var xmlString = @"<?xml version=""1.0"" encoding=""UTF-8""?>
    <foo>
       <row>
          <PHONE_NUM>124</PHONE_NUM>
          <PHONE_ORDER>1</PHONE_ORDER>      
       </row>
       <row>
          <PHONE_NUM>123</PHONE_NUM>
          <PHONE_ORDER>2</PHONE_ORDER>      
       </row>
       <row>
          <PHONE_NUM>181</PHONE_NUM>
          <PHONE_ORDER>3</PHONE_ORDER>      
       </row>   
    </foo>";

var report = XDocument.Parse(xmlString);
var newdoc = new XDocument();
newdoc.Add(new XElement("testsuites")); 
newdoc.Root.Add(report.Root);

但是上面的代码生成的是withou id并且命令books已经成为一个根:

<books>
  <foo>
      <row>
          <PHONE_NUM>124</PHONE_NUM> 
          <PHONE_ORDER>1</PHONE_ORDER> 
      </row>
      <row>
          <PHONE_NUM>123</PHONE_NUM> 
          <PHONE_ORDER>2</PHONE_ORDER> 
      </row>
      <row>
          <PHONE_NUM>181</PHONE_NUM> 
          <PHONE_ORDER>3</PHONE_ORDER> 
      </row>
      </foo>
  </books>

我想要实现的是:

<?xml version=""1.0"" encoding=""UTF-8""?>
<foo>
    <books id=""1"">
       <row>
          <PHONE_NUM>124</PHONE_NUM>
          <PHONE_ORDER>1</PHONE_ORDER>      
       </row>
       <row>
          <PHONE_NUM>123</PHONE_NUM>
          <PHONE_ORDER>2</PHONE_ORDER>      
       </row>
       <row>
          <PHONE_NUM>181</PHONE_NUM>
          <PHONE_ORDER>3</PHONE_ORDER>      
       </row>  
    </books>
</foo>

如果可以的话,要是用XmlDocument class就好了。任何帮助将不胜感激。

这里是关于你想做什么的,使用 XDocument class。这只是创建新节点并根据您的规范修改现有节点的问题:

var report = XDocument.Parse(xmlString);
report.Root.Name = XName.Get("books", report.Root.Name.NamespaceName);
report.Root.Add(new XAttribute("id", 1));
var newDoc = new XDocument();
var newRoot = new XElement("foo");
newRoot.Add(report.Root);
newDoc.Add(newRoot);

此过程可帮助您创建一个新子项并将所有节点移动到该新子项下。

  1. 首先创建一个新元素..
  2. 移动这个新元素下的所有节点
  3. 在 XML 中需要的地方添加新元素。
XmlDocument doc2 = new XmlDocument();
doc2.LoadXml(xmlString);

var newElement = doc2.CreateElement("body");
newElement.SetAttribute("id", "1");

var moveNodes = doc2.DocumentElement.SelectNodes("//row");
foreach(XmlNode node in moveNodes)
    newElement.AppendChild(node);

doc2.GetElementsByTagName("foo")[0].AppendChild(newElement);

输出

<?xml version="1.0" encoding="UTF-8"?>
<foo>
    <body id="1">
        <row>
            <PHONE_NUM>124</PHONE_NUM>
            <PHONE_ORDER>1</PHONE_ORDER>
        </row>
        <row>
            <PHONE_NUM>123</PHONE_NUM>
            <PHONE_ORDER>2</PHONE_ORDER>
        </row>
        <row>
            <PHONE_NUM>181</PHONE_NUM>
            <PHONE_ORDER>3</PHONE_ORDER>
        </row>
    </body>
</foo>