Array

How To Rename An XML Node Using C# XmlDocument

 

I recently hit an issue where i needed to change the name of an XML node. It ended up being a lot more complicated than I had expected it to be. node.Name is a read only field, so you can’t take the simple route and rename it this way. Since you cannot rename the node, I had to create a new node and delete the old one. Not overly complex, but it is a little messy with the limitations of the XmlDocument that require references to the old document. I also wanted to make this solution reusable. Here is the solution I came up with to rename an xml node using C# XmlDocument.

I will start with the method and explain why it seems overcomplicated for a simple task. The first parameter is the doc that oldRoot belongs to. The reason this needs to be passed is because you cant create an XmlNode without an XmlDocument and if you want to add a node to a document, it needs to be created with that document. This is why doc needs to be passed over. The for loop will take all of the elements from the old node and add them to the new node. This will mean that there are now 2 nodes that have the exact same content, but one has the new name that you wanted to rename the node to.

Once the new node has all the data of the old node, you can append this new node to the document and then remove the old one. This is all that you need to do. There is no real need for a return type here. You have passed doc as a parameter, any changes made to this doc will be made on a global level. When the method completes the change will have been made to the doc that you passed to this method.

            public static void renameXMLNode(XmlDocument doc, XmlNode oldRoot, string newname)
            {
                XmlNode newRoot = doc.CreateElement(newname);

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

Here is a simple scenario where you could call this method.

XmlDocument doc = new XmlDocument();
doc.Load("c:/xmlfile.xml");

var items = doc.SelectSingleNode("//Rows");
XmlNodeList rowlist = items.ChildNodes;

for (int i = 0; i <= rowlist.Count-1; i++)
{
    Helpers.renameXMLNode(doc, itemlist[0], "itemRow");
}

 

Related Articles

Related Questions

Function Keys Reversing Between Fn Actions And Normal

My keyboard has the usual F1 to F12 keys along the top. I use these for shortcuts in various applications. These keys also have...

Whirlpool Oven F6E6: Appliance Manager 1 Board Communication

I have a brand new Whirlpool oven W11I OM1 4MS2 H or (859991549450). I bought it alongside the microwave combi oven. I have had...

Whats the difference between the Tapo P100 and the P105?

There are a few different Tapo smart plugs. The P100 and P110 differ based on the smart power monitoring feature but where does the...

2 COMMENTS

  1. This code does not only rename the element, but always moves it down to become the last sibling. This will likely invalidate your xml document against its schema, if the child elements are in a sequence.
    This would be solved if your replaced
    parent.AppendChild(newRoot);
    by
    parent.InsertBefore(newRoot, oldRoot);

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Latest Tools

AI Content Detector

We've got this awesome free tool that'll help you figure out if that content you're looking at was written by a human or some...

Image Saturation

Are you looking for an easy-to-use, free app to modify your image saturation levels and make your pictures truly pop? Look no further! Our...

Pixelate Image Tool

Introducing the ultimate free online image pixelator tool that allows you to easily transform your images into stunning pixel art in just a few...

Image RGB Level Adjustment Tool

Introducing the ultimate image color adjustment tool for all your photo editing needs. Our free online tool lets you take full control of your...

Image Color Inverter

Looking for a quick and efficient way to convert your images into negatives? Our Free Image to Negative Converter is the answer! Our online...

Negative Image to Color Image Converter

Welcome to our Negative Image to Color Image Converter, a free and easy-to-use tool that helps you convert your old negative images into vibrant,...

Latest Posts

Latest Questions