Link to home
Start Free TrialLog in
Avatar of brian_dallas
brian_dallas

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of dualsoul
dualsoul

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial