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

Changing XML Tag Name

Hi,

I want to change tag name of my xmlDocument to another tag

for example:

<RootNode xmls:a="X">
    <a:A>
        <a:element>1</a:element>
    </a:A>
</RootNode>

I want to change it to

<RootNode xmls:a="X">
    <b:B xmls:b="Y">
        <a:element>1</a:element>
    </b:B>
</RootNode>

How can I change it using .NET?
Please let me know if you have any questions.

Thanks,
Fred
0
insanekid
Asked:
insanekid
  • 3
  • 2
1 Solution
 
avernetCommented:
Hi Fred,

My recommendation is to do this with XSLT. .NET comes with an XSLT engine, and you can use it to run the stylesheet below that does the transformation you want to do:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0" xmlns:a="X">
    <xsl:template match="a:A">
        <b:B xmlns:b="Y">
            <xsl:apply-templates select="@*|node()"/>
        </b:B>
    </xsl:template>
    <xsl:template match="@*|node()" priority="-100">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:transform>

I hope this helps.

Alex
--
Blog (XML, Web apps, Open Source):
http://www.orbeon.com/blog/


0
 
insanekidAuthor Commented:
Hi Alex,

Thanks for the quick reply...

I see...is there any other way, without using the stylesheet??

Thanks,
Fred

0
 
avernetCommented:
Hi Fred,

The other ways I know will include DOM manipulation using your favorite language (for instance C#). IMHO, XSLT is really the most appropriate language to do transformation of XML documents.

If you want to try doing some XSLT, have a look at this XSLT sandbox, where you can run XSLT on a web page without having to install anything on your computer:

http://www.orbeon.com/ops/goto-example/xslt

Alex
--
Blog (XML, Web apps, Open Source):
http://www.orbeon.com/blog/
0
 
insanekidAuthor Commented:
Hi Alex,

I'm asking about other alternatives is because if in case that instead of  simple

    <a:A>
        <a:element>1</a:element>
    </a:A>

 to

    <b:B xmls:b="Y">
        <a:element>1</a:element>
    </b:B>

with just one element inside the tag...there are more than one elements and perhaps there are complex type elements in it...wouldn't it be harder to do the translation?

Thanks,
Fred
0
 
avernetCommented:
Hi Fred,

XSLT is a language specialized to do XML transformations. You can do very complex things in XSLT; for instance I wrote once a chess program in XSLT. But it really excels at doing the type of transformation on XSLT document that you are talking about.

I would recommend you investigate this route. If XSLT is new to you, you will find a number of books on the subject, including the excellent "XSLT 2.0 Programmer's Reference" by Michael Kay (http://www.amazon.com/gp/product/0764569090). Of course, you will also find help here on EE.

I hope this helps,

Alex
--
Blog (XML, Web apps, Open Source):
http://www.orbeon.com/blog/
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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