How to Rename an XmlNode and all its Children

I'm trying to figure out the best way to rename an element and all of its children. For example, I would like to prepend or append arbitrary text to distinguish the node (and its children) for use in a DataSet. The code below is in .NET. The place I'm having a problem is noted below. Is there a better way to do this, XSLT perhaps?

BTW: I have to do this in C# .NET, so DOM3 isn't an option.

private void ProcessChildren(XmlNode xnod, int Depth)
{
   if ((xnod.NodeType == XmlNodeType.Element))
   {
       // WHAT DO I DO HERE TO RENAME THE CURRENT NODE???

       // Process the children of this node
       XmlNode xnodworking;
       if (xnod.HasChildNodes)
       {
            xnodworking = xnod.FirstChild;
            while(xnodworking != null)
            {
                  ProcessChildren(xnodworking, Depth + 1);
                  xnodworking = xnodworking.NextSibling;
            }
        }
    }
}

private XmlNode RenameNodes(System.Xml.XmlNode rs_node)
{
    XmlNode xnodWorking;
    if (rs_node.HasChildNodes)
    {
        xnodWorking = rs_node.FirstChild;
        while(xnodWorking != null)
        {
            ProcessChildren(xnodWorking, 0);
            xnodWorking = xnodWorking.NextSibling;
        }
     }
     return null;
}

Thanks.
brian_dallasAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dualsoulCommented:
> Is there a better way to do this, XSLT perhaps
i'm not working with .NET now, so i show you how to use XSLT for your task, and you decide what's better for you: XSLT or hand coded DOM manipulations

so...for instance you have xml, like that:
     <news>
           <title>some title</title>
           <author>some name</author>
           <message>some message</message>
       </news>
and want to rename:
     news -> NEWS
     title   -> TITLE
     author -> AUTHOR
      message -> MESSAGE

your XSLT will be:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
            <xsl:for-each select="news">
                      <NEWS>
                           <xsl:for-each select="title">                      
                              <TITLE>
                                    <xsl:value-of select="."/>
                               </TITLE>
                            </xsl:for-each>    

                           <xsl:for-each select="author">                      
                              <AUTHOR>
                                    <xsl:value-of select="."/>
                               </AUTHOR>
                            </xsl:for-each>    

                           <xsl:for-each select="message">                      
                              <MESSAGE>
                                    <xsl:value-of select="."/>
                               </MESSAGE>
                            </xsl:for-each>    

                      </NEWS>
             </xsl:for-each>
     </xsl:template>
</xsl:stylesheet>
         

certanly you can write common XSLT, which will not use particular node-names, and will rename all nodes, using some rule, for instance add a prefix for all names.

Hope it helps.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sparkplugCommented:
Hi,

There is no renameNode() method in the XML DOM. The only way to rename nodes using the DOM is to create a new node with the new name, copy all the child nodes and attributes from the old node to the new node, then replace the old node with the new node. Below is an example of how to do this using javascript. It should not be too difficult to translate this to C#.

sXML='<root><OldName id="2"><node2 id="3">test</node2></OldName></root>';

// load DOM
oXMLDom = new ActiveXObject("MSXML2.DOMDocument.4.0");
oXMLDom.async = false;
oXMLDom.loadXML(sXML);

//select old node
oOldNode = oXMLDom.selectSingleNode("//OldName");

//create new node
oNewNode = oXMLDom.createElement("NewName");

///copy attributes
for (i=0; i< oOldNode.attributes.length; i++)
{
      oNewNode.setAttributeNode(oOldNode.attributes[i].cloneNode(true));
}

//copy child nodes
for (i=0; i< oOldNode.childNodes.length; i++)
{
      oChildNode = oOldNode.childNodes[i].cloneNode(true);
      oNewNode.appendChild(oChildNode);
}


//replace nodes
oOldNode.parentNode.replaceChild(oNewNode, oOldNode);


alert(oXMLDom.xml);


XSLT may be a better approach as it is more flexible in that you can easily rename all ocurrences of a particular node. The following XSLT renames any nodes called "OldName" to "NewName" and renames the node "/Root/Node/OldName2" to "NewName2". This example also ensures that all the child nodes and attributes are retained.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- identity transform -->
<xsl:template match="@* | node()">
     <xsl:copy>
           <xsl:apply-templates select="@* | node()"/>
     </xsl:copy>
</xsl:template>

<!-- rename all nodes called OldName -->
<xsl:template match="OldName">
     <NewName>
           <xsl:apply-templates/>
     </NewName>
</xsl:template>

<!-- rename a specific node -->
<xsl:template match="/Root/Node/OldName2">
     <NewName2>
           <xsl:apply-templates/>
     </NewName2>
</xsl:template>


</xsl:stylesheet>


>S'Plug<





0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.