且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#:如何从 XML 元素中删除命名空间信息

更新时间:2023-12-03 18:59:10

尽管 Zombiesheep 给出了警告性的回答,我的解决方案是使用 xslt 转换来清洗 xml 以执行此操作.

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">
  <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>