• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 265
  • Last Modified:

Renaming nodes in an XML file

Hello,
I wrote a wrapper to consume a proprietary web service. It works good except the xml that the web service returns has some child nodes that are the same as the parent node so my dataset ignores the child.
Since I can't get the source to the webservice to change it, Would there be way I could loop through the file aand change either one so they are not the same?  If I manually run the service, save the file, and edit the value it works fine.  Here is an example
<responsible>
     <responsibleid>123456789</responsibleid>
    <responsible>John Smith</responsible>
</responsible>
The web services returns it as a string so this is what I'm using to put it in a dataset
public DataSet tickets()
        {
            EntryWrapper.entry.Service1 ticket = new EntryWrapper.entry.Service1();
            ticket.Credentials = new System.Net.NetworkCredential("name", "password", "OGI");
            string strWeb = ticket.ReportTest("F241A905-89B1-416d-8DC2-8C2EA1A6B434", "");
            XmlTextReader xml_reader = new XmlTextReader(new StringReader(strWeb));
            DataSet relational_data = new DataSet("ogi_team");
            relational_data.ReadXml(xml_reader);
            return relational_data;
        }
Thanks for any help
0
ChadMarsh
Asked:
ChadMarsh
1 Solution
 
rgavrilovCommented:
1. You define an XSL-tranformation for the transformation you want to accomplish.
2. You store it as an embedded resource in the project.
3. You load it and apply to your XML.
-------
1. XSLT. Following will do what you want, if you are OK with XSLT you might refactor it to look better, otherwise it will work as it is (renames '/responsible/responsible' to '/responsible/new_name_responsible'):
<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml"/>
  <xsl:template match="/">
    <xsl:for-each select="responsible">
      <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:copy-of select="responsibleid"/>
        <xsl:for-each select="responsible">
          <xsl:element name="new_name_responsible">
            <xsl:copy-of select="@*|*"/>
          </xsl:element>
        </xsl:for-each>
      </xsl:copy>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

2. Optionally you can cut'n'paste XSLT above into the source code as a string constant.

3. Here is how you apply XSLT transformation to your XML (.NET 2.0) (this is the entire program.cs file):
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Xsl;

namespace delitEditXml {
    internal class Program {
        private static void Main(string[] args) {
            XmlDocument doc = new XmlDocument();
            string inputXml = "<?xml version='1.0'?><responsible><responsibleid/><responsible/></responsible>";
            doc.LoadXml(inputXml);
            string xslt =
                @"<?xml version='1.0' encoding='utf-8'?>
                    <xsl:stylesheet version='1.0'
                        xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
                      <xsl:output method='xml'/>
                      <xsl:template match='/'>
                        <xsl:for-each select='responsible'>
                          <xsl:copy>
                            <xsl:copy-of select='@*'/>
                            <xsl:copy-of select='responsibleid'/>
                            <xsl:for-each select='responsible'>
                              <xsl:element name='new_name_responsible'>
                                <xsl:copy-of select='@*|*'/>
                              </xsl:element>
                            </xsl:for-each>
                          </xsl:copy>
                        </xsl:for-each>
                      </xsl:template>
                    </xsl:stylesheet>
                    ";

            Console.Out.WriteLine("Input:{0}", inputXml);

            XslCompiledTransform xslTransform = new XslCompiledTransform(false);
            xslTransform.Load(XmlReader.Create(new StringReader(xslt)));
            StringBuilder result = new StringBuilder();
            XmlWriter output = XmlWriter.Create(result);
            xslTransform.Transform(doc, output);

            Console.Out.WriteLine("Output:{0}", result.ToString());
        }
    }
}
0
 
ChadMarshAuthor Commented:
Excellent! Thanks.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now