[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

how do you rename an xml nodfe in .c# 2005

Posted on 2009-05-18
7
Medium Priority
?
593 Views
Last Modified: 2013-12-17
how do you rename an xml node in .c# 2005?
kind regards
Peter

I want
<ReturnData>
    <SomeNode></SomeNode
</ReturnData>

to be...

<PassedInData>
    <SomeNode></SomeNode
</PassedInData>
0
Comment
Question by:peter3244
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 9

Expert Comment

by:tetorvik
ID: 24410102
You could try something like in the snippet below:
            XmlDocument doc = new XmlDocument();
            //TODO: load your document here
 
            string newName = "newName";
            XmlElement newElement = doc.CreateElement(newName); //create new element
            XmlElement oldElement = null; // TODO:!! get reference to the element replaced here.
 
            while (oldElement.HasAttributes) //copy attributes
                newElement.SetAttributeNode(oldElement.RemoveAttributeNode(oldElement.Attributes[0]));
 
            while (oldElement.HasChildNodes) //copy child nodes
                newElement.AppendChild(oldElement.FirstChild);
 
            if (oldElement.ParentNode != null) //replace node
                oldElement.ParentNode.ReplaceChild(newElement, oldElement);

Open in new window

0
 

Author Comment

by:peter3244
ID: 24410630
Hi, i Have this solution under another post which seems easier. It you use a n XSLT file and do a transform. Although I'll kepp you posted on whether it works! I'll keep you posted


<?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="ReturnData">
		<PassedInData>
			<xsl:apply-templates/>
		</PassedInData>
	</xsl:template>
</xsl:stylesheet>

Open in new window

0
 
LVL 39

Expert Comment

by:abel
ID: 24410644
The most obvious way of transforming from one XML format to another. However, when it is really only one element that needs renaming, it isn't that much work using C#. Here's how I would do it with an XmlTextReader. The good news is: you do not have to load the DOM (which is slow, but insignificant if your documents are < 1MB).
 
 You already have a working solution above, here's my solution with XmlTextReader/Writer, which is faster then either XSLT or DOM, and doesn't consume any significant memory. Note that extension method, I needed that because the most obvious of all generic WriteXXX methods is not available, so I rolled my own:

// the method for reading / writing, note that the while-loop is all you need:
private void Q24416860_Click(object sender, EventArgs e)
{
    // create your xmlReader. You can use a file on disk, of course
    XmlTextReader xmlReader = new XmlTextReader(new StringReader(
            @"<ReturnData>
                <SomeNode></SomeNode>
            </ReturnData>"));
 
    // replace the .Create() with a filename in the argument to save it to disk
    StringBuilder xmlSB = new StringBuilder();
    XmlTextWriter xmlWriter = new XmlTextWriter(new StringWriter(xmlSB));
 
    while (xmlReader.Read())
    {
        if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.Name == "ReturnData")
            xmlWriter.WriteStartElement("PassedInData");
        else
            xmlWriter.WriteCurrent(xmlReader);
        
    }
    xmlWriter.Flush();
    Debug.WriteLine(xmlSB.ToString());
}

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 39

Accepted Solution

by:
abel earned 2000 total points
ID: 24410662
Aha, I see you found out about XSLT yourself as well! Good, I love XSLT ;-)

There's a small error in your code, which with your current sample wouldn't matter, but if you would have attribute nodes (or processing instructions, or comments) on that very same element you wanted to rename you would be in trouble, they would get lost. It is a tiny difference, here you go:

<?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="ReturnData">
       <PassedInData>
           <xsl:apply-templates select="@* | node()" />
       </PassedInData>
   </xsl:template>
</xsl:stylesheet>

Open in new window

0
 
LVL 39

Expert Comment

by:abel
ID: 24410673
And, in my enthusiasm to help you with the XSLT part, I totally forget to add the extension method. Here you are, just place it somewhere and make sure you "using" it on top of your class if you put it in a separate class (the code is not complete, but it serves 99% of all cases well):


public static class XmlWriterExtensions
{
    /// <summary>
    /// Writes the current information only (not the contents of the node,
    /// use WriteNode for that).
    /// </summary>
    /// <param name="writer">any XmlWriter</param>
    /// <param name="reader">the reader in Read state (Interactive ReadState)</param>
    public static void WriteCurrent(this XmlWriter writer, XmlReader reader)
    {
        switch (reader.NodeType)
        {
            case XmlNodeType.EndElement:
                writer.WriteEndElement();
                break;
            case XmlNodeType.Element:
                writer.WriteStartElement(reader.Name, reader.NamespaceURI);
                break;
            case XmlNodeType.Comment:
                writer.WriteComment(reader.Value);
                break;
            case XmlNodeType.CDATA:
                writer.WriteCData(reader.Value);
                break;
            case XmlNodeType.Attribute:
                writer.WriteAttributeString(reader.Name, reader.NamespaceURI, reader.Value);
                break;
            case XmlNodeType.Document:
                writer.WriteStartDocument();
                break;
            case XmlNodeType.Text:
                writer.WriteString(reader.Value);
                break;
            case XmlNodeType.Whitespace:
                writer.WriteWhitespace(reader.Value);
                break;
            default:
                break;
        }
    }
}

Open in new window

0
 

Author Closing Comment

by:peter3244
ID: 31582513
Many thanks, as I have put the code in a utilities class this will help in the future.
0
 
LVL 39

Expert Comment

by:abel
ID: 24431033
> Many thanks, as I have put the code in a utilities class this will help in the future.
good to hear that and glad it all works now :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question