Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Renaming nodes in an XML file

Posted on 2007-04-05
2
Medium Priority
?
262 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

636 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