【问题标题】:Renaming xmlnodes using c# dynamically使用c#动态重命名xmlnodes
【发布时间】:2013-03-28 00:24:37
【问题描述】:

我正在使用以下代码动态重命名 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");

                    }
        }

【问题讨论】:

  • 字符串是不可变的!!!
  • 1.老实说,重新创建另一个 XML 树可能更容易。 2. 确保您使用的所有内容都支持同一级别的同名节点。这不能假设。曾经。 3. 研究遍历 XML 树的递归。它可能更复杂、更密集,但在许多情况下可能效果更好。

标签: c# asp.net xml xmldocument


【解决方案1】:

因为您不能替换 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);

【讨论】:

  • 我可以将根名称 NewDataSet 更改为 rows。
  • 编辑了我的答案。无论如何..当我重新思考我认为的问题时,也许 - 取决于这个解决方案的“现实世界使用” - 转换你的 xml 输入的更优雅的方法是使用这里讨论的 XSL 转换:link
  • 如果您没有绑定到 XmlDocument 并且可以使用 .NET Framwork 3.5+ XDocument,它确实更加漂亮和实用,正如 Jeff Mercado 的回答中所见。跨度>
【解决方案2】:

拥抱 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);

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-06
      • 2013-04-24
      • 1970-01-01
      • 2016-08-28
      • 1970-01-01
      • 2023-01-30
      相关资源
      最近更新 更多