代码之家  ›  专栏  ›  技术社区  ›  Peter Stegnar

如何使用C#从XML中删除所有名称空间?

  •  91
  • Peter Stegnar  · 技术社区  · 17 年前

    我正在寻找一个干净、优雅、智能的解决方案来从所有XML元素中删除名称空间?功能将如何做到这一点?

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

    要从中删除NS的示例XML:

    <?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)后,我们应该得到:

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

    首选的解决方案语言是C#on.NET3.5SP1。

    29 回复  |  直到 17 年前
        1
  •  106
  •   Termininja Hassan Rahman    9 年前
        2
  •  65
  •   Dexter Legaspi    12 年前
        3
  •  34
  •   JSC    13 年前
        4
  •  28
  •   Jimmy    17 年前
        5
  •  16
  •   Florian Dürrbaum    15 年前
        6
  •  10
  •   annakata    17 年前
        7
  •  10
  •   Himanshu THE ONLY ONE    13 年前
        8
  •  9
  •   jocull    15 年前
        9
  •  8
  •   Konrad Morawski    14 年前
        10
  •  6
  •   Community Mohan Dere    9 年前
        11
  •  4
  •   Philip Atz    13 年前
        12
  •  3
  •   JackG    14 年前
        13
  •  2
  •   Visar    9 年前
        14
  •  2
  •   MarcE    7 年前
        15
  •  1
  •   andygjp    15 年前
        16
  •  1
  •   user892217    13 年前
        17
  •  1
  •   user2740574    12 年前
        18
  •  1
  •   esteewhy    12 年前
        19
  •  1
  •   Jeff Mercado    12 年前
        20
  •  1
  •   Abacus painotpi    11 年前
        21
  •  1
  •   Wolf5    11 年前
        22
  •  1
  •   sww    10 年前
        23
  •  1
  •   Si Livan    10 年前
        24
  •  0
  •   user2789366    12 年前
        25
  •  0
  •   Scott Parker    10 年前
        26
  •  0
  •   LeeT    9 年前
        27
  •  0
  •   Stas BZ    9 年前
        28
  •  0
  •   user2056154    7 年前
        29
  •  -1
  •   Siddharth    13 年前
        30
  •  -1
  •   Leonid    12 年前