Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / February 2008

Tip: Looking for answers? Try searching our database.

Namespace Issue with InnerXML

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Antoine Fromentin - 17 Jan 2008 15:30 GMT
Using C# I need to remove the root element of an xml document and keep the
inner XML document as is.x

<root xmlns="http://pjc.BizTalk">
<ns0:LaunchStoreUpload xmlns:ns0="http://pjc.schemas">
 <Source>
   <FTPParams>A</FTPParams>
 </Source>
</ns0:LaunchStoreUpload>
</root>

Whatever I try :
   root.RemoveAttribute("xmlns", "http://pjc.BizTalk");
   root.RemoveAllAttributes();
   root.SetAttribute("xmlns", "");

when I use the root.InnerXML I obtain the following result

<ns0:LaunchStoreUpload xmlns:ns0="http://pjc.schemas">
 <Source xmlns="http://pjc.BizTalk">
   <FTPParams>A</FTPParams>
 </Source>
</ns0:LaunchStoreUpload>

That's really not good for me, I don't want the extra
xmlns=http://pjc.BizTalk added in the <Source> node

Any solution would be very appreciated !
Thanks,
Antoine
Martin Honnen - 17 Jan 2008 15:53 GMT
> Using C# I need to remove the root element of an xml document and keep the
> inner XML document as is.x
[quoted text clipped - 22 lines]
> That's really not good for me, I don't want the extra
> xmlns=http://pjc.BizTalk added in the <Source> node

Sorry, your approach can't work, the namespace of an node in the DOM
model is determined by its NamespaceURI property and you can't change
that. Deleting any xmlns attributes on ancestors does not help, you
can't change the namespace of a node.
You might want to look into XSLT to transform the XML. If you need help
with that then let us know.

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

Antoine Fromentin - 17 Jan 2008 19:44 GMT
Thanks,
I know understand why I would need to transform the XML using XSLT.

I'm sure it's probably not a complex XSLT that can remove the root element
of an XML file but as I never used XMLT....
My real XML file is more complex , I'd like to create a generic XSLT which
only removes the root element.

Does someone knows a good reference to work with...

Thanks,
Antoine

>> Using C# I need to remove the root element of an xml document and keep
>> the inner XML document as is.x
[quoted text clipped - 29 lines]
> You might want to look into XSLT to transform the XML. If you need help
> with that then let us know.
Martin Honnen - 18 Jan 2008 13:28 GMT
> Thanks,
> I know understand why I would need to transform the XML using XSLT.
[quoted text clipped - 3 lines]
> My real XML file is more complex , I'd like to create a generic XSLT which
> only removes the root element.

But your original change is more than only removing the root element,
you want to change the namespace of the descendant nodes. When you have e.g.
  <root xmlns="http://example.com/"><child><grandchild/></child></root>
then the default namespace declaration on the root element applies to
the descendant elements as well thus simply remving the root element
will leave the elements child and grandchild in the default namespace
meaning a serialization would look like
  <child xmlns="http://example.com/"><grandchild/></child>
while your orginal request was to strip the namespace from the descendants.
Furthermore it is generally not possible to remove the root element and
get a well-formed document as the root element can have several child
elements and that way the result could have several new root elements
which is not allowed for an XML document.

Here is an XSLT stylesheet

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0"
  xmlns:bt="http://pjc.BizTalk"
  exclude-result-prefixes="bt">

  <xsl:template match="/bt:*">
    <xsl:apply-templates select="*[1]"/>
  </xsl:template>

  <xsl:template match="bt:*">
    <xsl:element name="{local-name()}">
      <xsl:apply-templates select="@* | node()"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="*">
    <xsl:element name="{name()}" namespace="{namespace-uri()}">
      <xsl:apply-templates select="@* | node()"/>
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>

that applied to your original XML document

<root xmlns="http://pjc.BizTalk">
<ns0:LaunchStoreUpload xmlns:ns0="http://pjc.schemas">
  <Source>
    <FTPParams>A</FTPParams>
  </Source>
</ns0:LaunchStoreUpload>
</root>

gives the result

<ns0:LaunchStoreUpload xmlns:ns0="http://pjc.schemas">
  <Source>
    <FTPParams>A</FTPParams>
  </Source>
</ns0:LaunchStoreUpload>

The first template makes sure that only the first child element of the
input root element is processed, the second template strips the
namespace of all elements in the namespace http://pjc.BizTalk and the
third template makes sure that elements in other namespaces are copied
through unchanged.

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

Antoine Fromentin - 23 Jan 2008 15:38 GMT
Danke Martin,

My issue is now that the XSL doesn't preserve the attributes !

I found XSL not easy to understand but the W3C Recommandation helps a lot :
http://www.w3.org/TR/xslt

Thanks,
Antoine
Montreal

>> Thanks,
>> I know understand why I would need to transform the XML using XSLT.
[quoted text clipped - 67 lines]
> of all elements in the namespace http://pjc.BizTalk and the third template
> makes sure that elements in other namespaces are copied through unchanged.
Martin Honnen - 23 Jan 2008 16:06 GMT
> My issue is now that the XSL doesn't preserve the attributes !

Add a template for that:

  <xsl:template match="@* | text() | comment() | processing-instruction()">
    <xsl:copy/>
  </xsl:template>

Together with the other templates that should ensure that attributes and
text and comments and processing instructions are copied through.

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

Amy - 06 Feb 2008 15:11 GMT
> Using C# I need to remove the root element of an xml document and keep the
> inner XML document as is.x
[quoted text clipped - 26 lines]
> Thanks,
> Antoine

I want to remove the namespace on the descendent nodes.  I have tried many things also like node.Attributes.RemovedNamedItem("NamespaceURI");
as listed by you.  
In my XML doc I have a need to add entries with no value for the namespace.  
What I currently have is this:
add switchValue="Error" name="Gary" xmlns="">
I would like it to be this:
add switchValue="Error" name="Gary">.

Is this similiar to your situtation and how do you solve this?  
How did you use a stylesheet to resolve this?  
Thanks,
Amy

BizTalk Utilities - Frustration free BizTalk Adapters
http://www.topxml.com/biztalkutilities

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.