?
Solved

Renaming nodes in an XML file

Posted on 2007-04-05
2
Medium Priority
?
255 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
[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
2 Comments
 
LVL 1

Accepted Solution

by:
rgavrilov earned 2000 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
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…
Suggested Courses

770 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