我正在寻找干净,优雅和智能的解决方案来从所有XML元素中删除名称空间?如何做到这一点的功能?
定义的界面:
public interface IXMLUtils
{
string RemoveAllNamespaces(string xmlDocument);
}
Run Code Online (Sandbox Code Playgroud)
示例XML从以下位置删除NS:
<?xml version="1.0" encoding="utf-16"?>
<ArrayOfInserts xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<insert>
<offer xmlns="http://schema.peters.com/doc_353/1/Types">0174587</offer>
<type2 xmlns="http://schema.peters.com/doc_353/1/Types">014717</type2>
<supplier xmlns="http://schema.peters.com/doc_353/1/Types">019172</supplier>
<id_frame xmlns="http://schema.peters.com/doc_353/1/Types" />
<type3 xmlns="http://schema.peters.com/doc_353/1/Types">
<type2 />
<main>false</main>
</type3>
<status xmlns="http://schema.peters.com/doc_353/1/Types">Some state</status>
</insert>
</ArrayOfInserts>
Run Code Online (Sandbox Code Playgroud)
在我们调用RemoveAllNamespaces(xmlWithLotOfNs)之后,我们应该得到:
<?xml version="1.0" encoding="utf-16"?>
<ArrayOfInserts>
<insert>
<offer >0174587</offer>
<type2 >014717</type2>
<supplier >019172</supplier>
<id_frame />
<type3 >
<type2 />
<main>false</main>
</type3>
<status >Some state</status>
</insert>
</ArrayOfInserts>
Run Code Online (Sandbox Code Playgroud)
解决方案的优先语言是.NET 3.5 SP1上的C#.
我正在从XML文件创建一个Dom(org.w3c.dom.Document)文档.我想从该文档中删除所有命名空间以调用其他一些服务.该服务期望XML没有名称空间.