Solved

Renaming nodes in an XML file

Posted on 2007-04-05
2
247 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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 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