如何使用 C# 从 XML 中删除所有命名空间? [英] How to remove all namespaces from XML with C#?

查看:53
本文介绍了如何使用 C# 从 XML 中删除所有命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找从所有 XML 元素中删除命名空间的干净、优雅和智能的解决方案?这样做的功能如何?

I am looking for the clean, elegant and smart solution to remove namespacees from all XML elements? How would function to do that look like?

定义接口:

public interface IXMLUtils
{
        string RemoveAllNamespaces(string xmlDocument);
}

要从中删除 NS 的示例 XML:

Sample XML to remove NS from:

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

在我们调用 RemoveAllNamespaces(xmlWithLotOfNs) 之后,我们应该得到:

After we call RemoveAllNamespaces(xmlWithLotOfNs), we should get:

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

首选的解决方案语言是 .NET 3.5 SP1 上的 C#.

Preffered language of solution is C# on .NET 3.5 SP1.

推荐答案

好吧,这是最终答案.我使用了很棒的 Jimmy 想法(不幸的是它本身并不完整)和完整的递归函数来正常工作.

Well, here is the final answer. I have used great Jimmy idea (which unfortunately is not complete itself) and complete recursion function to work properly.

基于接口:

string RemoveAllNamespaces(string xmlDocument);

我在这里代表了用于删除 XML 命名空间的最终干净和通用的 C# 解决方案:

I represent here final clean and universal C# solution for removing XML namespaces:

//Implemented based on interface, not part of algorithm
public static string RemoveAllNamespaces(string xmlDocument)
{
    XElement xmlDocumentWithoutNs = RemoveAllNamespaces(XElement.Parse(xmlDocument));

    return xmlDocumentWithoutNs.ToString();
}

//Core recursion function
 private static XElement RemoveAllNamespaces(XElement xmlDocument)
    {
        if (!xmlDocument.HasElements)
        {
            XElement xElement = new XElement(xmlDocument.Name.LocalName);
            xElement.Value = xmlDocument.Value;

            foreach (XAttribute attribute in xmlDocument.Attributes())
                xElement.Add(attribute);

            return xElement;
        }
        return new XElement(xmlDocument.Name.LocalName, xmlDocument.Elements().Select(el => RemoveAllNamespaces(el)));
    }

它 100% 工作,但我没有对其进行太多测试,因此它可能无法涵盖某些特殊情况......但它是一个很好的开始.

It's working 100%, but I have not tested it much so it may not cover some special cases... But it is good base to start.

这篇关于如何使用 C# 从 XML 中删除所有命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆