Skip to main content

How to rename an XML Node in C#


This was driving me crazy - here's an easy cut and paste solution to not being able to use the DOM to rename a node for lazy developers like me:

public static XmlNode RenameNode (XmlNode node, string namespaceURI,string qualifiedName)
{
if (node.NodeType == XmlNodeType.Element)
{
XmlElement oldElement = (XmlElement) node;
XmlElement newElement =
node.OwnerDocument.CreateElement(qualifiedName, namespaceURI);

while (oldElement.HasAttributes)
{
newElement.SetAttributeNode(oldElement.RemoveAttributeNode(oldElement.Attributes[0]));
}

while (oldElement.HasChildNodes)
{
newElement.AppendChild(oldElement.FirstChild);
}

if (oldElement.ParentNode != null)
{
oldElement.ParentNode.ReplaceChild(newElement, oldElement);
}

return newElement;
}
else
{
return null;
}
}

Ahh. That's better :)

Comments

  1. Just saw your post too late and developed my own solution!

    The following works given that you are iterating over some of the nodes in the document, and have stored them in an ArrayList of XmlNode's called 'oldNodes'. I had to use indexed access to the ArrayList rather than iteration (with a foreach), as the latter had 'unexpected' results!


    for(int i=0; i < oldNodes.Count; i++)
    {
    XmlDocumentFragment f = doc.CreateDocumentFragment();
    XmlNode old = oldNodes[i] as XmlNode;
    f.InnerXml = old.OuterXml.Replace(old.Name, newName);
    old.ParentNode.ReplaceChild(f.ChildNodes[0], old);
    }

    Eoghan http://eoghan.qatano.org/

    ReplyDelete
  2. Anonymous7:12 am

    Great piece of reusable code. Saved me some time. Thanks!!

    ReplyDelete
  3. Stiefel12:34 am

    Sorry Eoghan, but i think your code fails if the tag-name is used in some child node again -> in this case all tags are renamed ..

    ReplyDelete
  4. Anonymous10:14 am

    Just noticed that you posted this in 2006...well, it still works great in 2011! Thanks!

    ReplyDelete
  5. Anonymous11:03 pm

    public static XmlNode RenameNode (XmlNode node, string namespaceURI,string qualifiedName)
    {
    if (node.NodeType == XmlNodeType.Element)
    {
    XmlElement oldElement = (XmlElement) node;
    XmlElement newElement =
    node.OwnerDocument.CreateElement(qualifiedName, namespaceURI);

    while (oldElement.HasAttributes)
    {
    newElement.SetAttributeNode(oldElement.RemoveAttributeNode(oldElement.Attributes[0]));
    }

    while (oldElement.HasChildNodes)
    {
    newElement.AppendChild(oldElement.FirstChild);
    }

    if (oldElement.ParentNode != null)
    {
    oldElement.ParentNode.ReplaceChild(newElement, oldElement);
    }

    return newElement;
    }
    else
    {
    return null;
    }
    }

    ReplyDelete
  6. 3 issues:

    1. Signature should be changed to
    public static XmlNode RenameNode (this XmlElement node, string namespaceURI,string qualifiedName)

    this allow 1, remove the unncessery check, and use the code as extention method for XmlElements

    myElement.RenameNode("newName");


    2. there is a problem with references kept to the old node, which are NOT updated


    3. this is a very lengthy operation which traverse a lot of items (possibly) so use it only when REAALLY necessery.


    Other then that, Kudos.
    nice work.

    ReplyDelete
  7. Anonymous4:04 pm

    What if the oldElement has no parent node, i.e renaming an XmlElement directly under the XmlDocument as DocumentElement?

    ReplyDelete

Post a Comment

Popular posts from this blog

Easter at Blackhead

Camping out on the headland, miles from anywhere. Big swell, off-shore winds, clear crisp nights and smoke from the wood fire. Sometimes it's so rewarding to get out from under the pressure of your life and just be a family of humans.

Democracy in Action...

I have to vote in the US Senate Election Tomorrow. Okay, I don't have to vote, but being an Australian American, voting is something we do. Australia has compulsory voting, which I am overwhelmingly supportive of. People often assume that the point of compulsory voting is to somehow educate people about politics - to force them to have an opinion. The reality has nothing to do with voters, and everything to do with ensuring good governance - By forcing politicians to care about every voter, rather than just the ones who can be bothered to go down to the poll both and vote, it means that politicians are consequently forced to address the needs of all the citizens in the country. Under a non compulsory voting system, it's mainly the wealthy and educated who vote. I was a little shocked to find out that part of the aim of the campaign advertisements that have been confusing me for the last month on TV is not to encourage voters to vote for one candidate or another, but to discou...