Change the node names in an XML file using C#

后端 未结 8 1867
南笙
南笙 2020-12-09 17:51

I have a huge bunch of XML files with the following structure:


  someContent
  someType
&         


        
8条回答
  •  [愿得一人]
    2020-12-09 18:10

    I used this method to rename the node:

    /// 
    /// Rename Node
    /// 
    /// 
    /// 
    /// 
    private static void RenameNode(XmlNode parentnode, string oldChildName, string newChildName)
    {
        var newnode = parentnode.OwnerDocument.CreateNode(XmlNodeType.Element, newChildName, "");
        var oldNode = parentnode.SelectSingleNode(oldChildName);
    
        foreach (XmlAttribute att in oldNode.Attributes)
            newnode.Attributes.Append(att);
        foreach (XmlNode child in oldNode.ChildNodes)
            newnode.AppendChild(child);
    
        parentnode.ReplaceChild(newnode, oldNode);
    }
    

提交回复
热议问题