Solved

Renaming nodes in an XML file

Posted on 2007-04-05
2
243 Views
Last Modified: 2010-04-15
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
Comment
Question by:ChadMarsh
2 Comments
 
LVL 1

Accepted Solution

by:
rgavrilov earned 500 total points
ID: 18857937
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
 
LVL 2

Author Comment

by:ChadMarsh
ID: 18858309
Excellent! Thanks.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now