3

我有这种类型:

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

.NET XML 序列化陷阱从哪里XmlSerializableDictionary借来的?.

生成以下XmlSerializerXML:

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

我唯一希望摆脱的是根元素上的 xmlns 声明。我该怎么做?谢谢。

4

1 回答 1

1

通过XmlSerializerNamespaces

    var ns = new XmlSerializerNamespaces();
    ns.Add("", ""); // this line is important... honest!
    var ser = new XmlSerializer(type);
    ser.Serialize(destination, obj, ns);
于 2011-09-24T09:47:59.877 回答