C#:如何从 XML 元素中删除命名空间信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/413050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C#: How to remove namespace information from XML elements
提问by Marc
How can I remove the "xmlns:..." namespace information from each XML element in C#?
如何从 C# 中的每个 XML 元素中删除“xmlns:...”命名空间信息?
采纳答案by annakata
Zombiesheep's cautionary answer notwithstanding, my solution is to wash the xml with an xslt transform to do this.
尽管 Zombiesheep 给出了警告性回答,但我的解决方案是使用 xslt 转换清洗 xml 以执行此操作。
wash.xsl:
洗.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>
回答by NickBeaugié
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.
我有一个类似的问题(需要从特定元素中删除命名空间属性,然后将 XML 作为XmlDocument
BizTalk返回)但是一个奇怪的解决方案。
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.
在将 XML 字符串加载到XmlDocument
对象中之前,我进行了文本替换以删除有问题的命名空间属性。起初似乎是错误的,因为我最终得到了无法被 Visual Studio 中的“XML Visualizer”解析的 XML。这就是最初让我放弃这种方法的原因。
However, the text could still be loaded into the XmlDocument
and I could output it to BizTalk fine.
但是,文本仍然可以加载到XmlDocument
BizTalk 中,我可以将其输出到 BizTalk 中。
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!
还要注意,早些时候,我在尝试使用childNode.Attributes.RemoveAll()
删除命名空间属性时遇到了一个死胡同- 它又回来了!
回答by Simon
From here http://simoncropp.com/working-around-xml-namespaces
从这里http://simoncropp.com/working-around-xml-namespaces
var xDocument = XDocument.Parse(
@"<root>
<f:table xmlns:f=""http://www.w3schools.com/furniture"">
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
</root>");
xDocument.StripNamespace();
var tables = xDocument.Descendants("table");
public static class XmlExtensions
{
public static void StripNamespace(this XDocument document)
{
if (document.Root == null)
{
return;
}
foreach (var element in document.Root.DescendantsAndSelf())
{
element.Name = element.Name.LocalName;
element.ReplaceAttributes(GetAttributes(element));
}
}
static IEnumerable GetAttributes(XElement xElement)
{
return xElement.Attributes()
.Where(x => !x.IsNamespaceDeclaration)
.Select(x => new XAttribute(x.Name.LocalName, x.Value));
}
}