如何使用c#删除XML的第一个元素?

时间:2016-08-30 21:01:25

标签: c# xml

我需要删除第一个元素。 我有XmlDocument文档,它有这个xml。只需要知道如何删除第一个元素"?xml version =" 1.0"编码=" US-ASCII""

这需要

<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            version="1.0">
<xsl:template match="/*">
  <xsl:apply-templates select="*[1]"/>
</xsl:template>
<xsl:template match="@*|node()"><!--identity for all other nodes-->
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>
</xsl:stylesheet>
像这样

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            version="1.0">
<xsl:template match="/*">
  <xsl:apply-templates select="*[1]"/>
</xsl:template>
<xsl:template match="@*|node()"><!--identity for all other nodes-->
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

如果你有一个包含XML的XmlDocument对象,那么你可以在没有这样的XML声明的情况下获取XML:

string xml = doc.DocumentElement.OuterXml;

或者,您可以完全删除XmlDeclaration

if (doc.FirstChild is XmlDeclaration)
    doc.RemoveChild(doc.FirstChild);
string xml = doc.OuterXml;