简体   繁体   English

如何摆脱XmlSerializer插入的xmlns声明?

[英]How do I get rid of the xmlns declarations inserted by the XmlSerializer?

I have this type: 我有这种类型:

[Serializable, DataContract]
public class A
{
  [DataMember(Order = 1)]
  public int P1 { get; set; }
  [DataMember(Order = 2)]
  public XmlSerializableDictionary<string, string> P2 { get; set; }
}

Where XmlSerializableDictionary is borrowed from .NET XML serialization gotchas? XmlSerializableDictionary是从.NET XML序列化中获取的吗? .

The XmlSerializer produces the following XML: XmlSerializer生成以下XML:

<?xml version="1.0"?>
<A xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <P1>17</P1>
  <P2>
    <item>
      <key>
        <string>Hello World!</string>
      </key>
      <value>
        <string>xo-xo</string>
      </value>
    </item>
    <item>
      <key>
        <string>Good Bye!</string>
      </key>
      <value>
        <string>xi-xi</string>
      </value>
    </item>
  </P2>
</A>

The only things that I wish to get rid of are the xmlns declarations on the root element. 我希望摆脱的唯一事情是根元素上的xmlns声明。 How do I do it? 我该怎么做? Thanks. 谢谢。

Via XmlSerializerNamespaces : 通过XmlSerializerNamespaces

    var ns = new XmlSerializerNamespaces();
    ns.Add("", ""); // this line is important... honest!
    var ser = new XmlSerializer(type);
    ser.Serialize(destination, obj, ns);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM