使用c#动态重命名xmlnodes

时间:2013-04-05 14:25:58

标签: c# asp.net xml xmldocument

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

示例XML doucment

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

                    }
        }

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分:1)

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

答案 2 :(得分: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);