2

我正在使用以下代码动态重命名 xmlnode 名称。虽然 xml 循环很好,但它不会更改节点名称。请帮助我做到这一点。

示例 XML 文档

- <NewDataSet>
- <Table5>
  <FLD_ID>62</FLD_ID> 
  <FLD_DATE>2013-03-12</FLD_DATE> 
  <FLD_MOD_DATE>2013-04-05</FLD_MOD_DATE> 
  <FLD_DESC>New Creation</FLD_DESC> 
  </Table5>
- </NewDataSet>

需要的 XML 文档

- <rows>
- <row>
  <cell>62</cell> 
  <cell>2013-03-12</cell> 
  <cell>2013-04-05</cell> 
  <cell>New Creation</cell> 
  </row>
- </rows>

我的代码在这里

XmlNode PackageListNode = hst_doc.SelectSingleNode("NewDataSet");
                XmlNodeList PackageNodeList = PackageListNode.SelectNodes("Table5");

                foreach (XmlNode node in PackageNodeList)
                {
                    node.Name.Replace("Table5", "row");

                    foreach (XmlNode ls in node)
                    {
                        ls.Name.Replace(ls.Name, "cell");

                    }
        }
4

3 回答 3

3

由于您无法在XmlDocument... ...中替换元素名称,因此针对您的特定情况的替换方法:

string srcXML = "<NewDataSet><Table5><FLD_ID>62</FLD_ID><FLD_DATE>2013-03-12</FLD_DATE><FLD_MOD_DATE>2013-04-05</FLD_MOD_DATE><FLD_DESC>New Creation</FLD_DESC></Table5></NewDataSet>";
var doc = new XmlDocument();
doc.LoadXml(srcXML);

XmlNode oldRoot = doc.SelectSingleNode("NewDataSet");
XmlNode newRoot = doc.CreateElement("rows");
doc.ReplaceChild(newRoot, oldRoot);

foreach (XmlNode childNode in oldRoot.ChildNodes)
{
    newRoot.AppendChild(childNode.CloneNode(true));
}

XmlNodeList PackageNodeList = newRoot.SelectNodes("Table5");

foreach (XmlNode node in PackageNodeList)
{
    var newNode = doc.CreateElement("row");
    newRoot.ReplaceChild(newNode, node);

    foreach (XmlNode childNode in node.ChildNodes)
    {
        var clonedChildNode = childNode.CloneNode(true);
        newNode.AppendChild(clonedChildNode);

        var newChildNode = doc.CreateElement("cell");
        newNode.ReplaceChild(newChildNode, clonedChildNode);

        foreach (XmlNode childChildNode in clonedChildNode.ChildNodes)
        {
            newChildNode.AppendChild(childChildNode.CloneNode(true));
        }                    
    }
}

Debug.Print(doc.OuterXml);
于 2013-04-05T15:19:40.780 回答
1

拥抱 LINQ,拥抱它!

// load the document from a file
var doc = XDocument.Load(xmlPath);
var root = doc.Root;

// replace the root element with a new element
root.ReplaceWith(

    // create a new element with
    // the name "rows" with new children
    new XElement("rows",

        // replace all child elements of
        // the root with new elements
        root.Elements().Select(table =>

            // replace the current element with a new element
            // with the name "row" with the new children
            new XElement("row",

                // replace all child elements of the
                // current element with new elements
                table.Elements().Select(field =>

                    // replace the current element with a new element
                    // with the name "cell" with the same value
                    new XElement("cell",
                        (string)field
                    )
                )
            )
        )
    )
);

// save the document back to the file
doc.Save(xmlPath);
于 2013-04-06T02:29:32.907 回答
0

String.Replace 返回一个新字符串,所以当然会喜欢:

node.Name = node.Name.Replace("Table5", "row");

这也可能是

node.Name = "row";

但是,如果您查看文档,它说 XmlNode.Name 纯粹是“getter”而不是“setter”,因此您可能需要创建全新的节点来替换它们,这取决于实际的实现,因为XmlNode 是一个抽象类。

  for (int i = 0; i < PackageNodeList.Count; ++i) XmlNode node in PackageNodeList)
            {
                XmlNode replacementNode = new XmlNode("row");

                foreach (XmlNode ls in node)
                {
                    XmlNode newCell = new XmlNode("cell");
                    newCell.Value = ls.Value;
                    replacementNode.AppendChild(newCell);
                }
                PackageNodeList[i] = replacementNode
                PackageNodeList[i].ParentNode.ReplaceChild(PackageNodeList[i], replacementNode);
    }
于 2013-04-05T14:30:27.823 回答