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

2023-09-02 23:55:39 作者:觉得多事

我如何删除的xmlns:...?从每个XML元素命名空间信息在C#

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

推荐答案

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>