代码之家  ›  专栏  ›  技术社区  ›  Richard Garside

从.NET中生成的XML中删除命名空间[重复]

  •  12
  • Richard Garside  · 技术社区  · 16 年前

    可能重复:
    XmlSerializer: remove unnecessary xsi and xsd namespaces

    我正在使用XMLSerializer和用属性标记的类生成一些XML。此XML将发送到REST Web服务。

    它生成以下XML:

    <?xml version="1.0" encoding="utf-8"?>
    <person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <first-name>API</first-name>
      <last-name>TestPersonDeleteMe</last-name>
      <title>Delete me</title>
    </person>
    

    一切都会很好,除了我使用的Web服务不理解模式内容并抛出500个错误。

    是否有方法停止添加XmlSerializer 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"' 标签?

    1 回复  |  直到 14 年前
        1
  •  21
  •   marc_s MisterSmith    16 年前

    如果使用自定义序列化程序,请尝试此操作

    XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
    namespaces.Add(string.Empty, string.Empty);
    

    然后将Namespaces对象添加到序列化程序中。