views:

4882

answers:

4

How can I remove the "xmlns:..." namespace information from each XML element in C#?

+1  A: 

Be careful about what you are asking for. The namespace information in larger or more complex XML files is an integral part of the document map. Removing all namespaces may make navigation through the tree more problematic.

ZombieSheep
you're not wrong, but in practice namespaces do seem to cause a lot of problems
annakata
not only that - it may also create duplicate meanings and confusion as MyNamespace:Person and AnotherNamespace:Person are likely to describe something completely different
Yossi Dahan
@Yossi Dahan - That was kind of what I was driving at, but I guess I didn't explain my meaning. Thanks for the clarification.
ZombieSheep
Thanks ZombieSheep, I'm very much aware of this problem. However in my situation I know I can savely remove the namespace information.
Marc
@annakata: (re: "in practice namespaces do seem to cause a lot of problems "). What a grasp of the main XML concepts! I am impressed... Before supporting such a destructive idea, did you think to explain why it was bad? Or just wanted to get a few SO points by all means? Cheers,
Dimitre Novatchev
I've responded to this mindlessly offensive attack on the duplicate comment on my own answer, to wit: it's mindless and it's offensive.
annakata
+4  A: 

Zombiesheep's cautionary answer notwithstanding, my solution is to wash the xml with an xslt transform to do this.

wash.xsl:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  <xsl:output method="xml" indent="no" encoding="UTF-8"/>

  <xsl:template match="/|comment()|processing-instruction()">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

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

  <xsl:template match="@*">
    <xsl:attribute name="{local-name()}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>

</xsl:stylesheet>
annakata
Thank you very much for this. That's exactly what I was looking for: Now I first transform the XML with this XSL, then I apply my XSL on the output. You saved my day!
Marc
no worries - keep it in your XSLT toolbox :)
annakata
@annakata: (re: "in practice namespaces do seem to cause a lot of problems "). What a grasp of the main XML concepts! I am impressed... Before supporting such a destructive idea, did you think to explain why it was bad? Or just wanted to get a few SO points by all means? Cheers,
Dimitre Novatchev
@Dimitre - how incredibly offensive. If you'd like to bring something of value to the table, perhaps you could support the assertion that this is going to be destructive rather than attack the self-evident (i.e. this question exists) fact that namespaces can be a problem. "Cheers" to you to idiot.
annakata
A: 

Can't you just read the file, find and remove all xmlns="..."? Why should it be hard?

Rumen Georgiev
For one - because if xmlns is used to define a prefix, that prefix will then be used in elements throughout the document
Yossi Dahan
A: 

I had a similar problem (needing to remove a namespace attribute from a particular element, then return the XML as an XmlDocument to BizTalk) but a bizarre solution.

Before loading the XML string into the XmlDocument object, I did a text replacement to remove the offending namespace attribute. It seemed wrong at first as I ended up with XML that could not be parsed by the "XML Visualizer" in Visual Studio. This is what initially put me off this approach.

However, the text could still be loaded into the XmlDocument and I could output it to BizTalk fine.

Note too that earlier, I hit one blind alley when trying to use childNode.Attributes.RemoveAll() to remove the namespace attribute - it just came back again!

NickBeaugié