为DataContractSerializer预定义XML名称空间

rek*_*ire 7 c# xml-serialization xml-namespaces

我正在构建一个自托管的WCF服务.我正在构建一个特殊的数据结构,以实现非常灵活的数据传输.到目前为止,我测试我的结构是否可以使用DataContractSerializer进行序列化.这工作正常,我很高兴,但有些东西让我烦恼:

在我的XML输出中有许多重新定义的xmlns属性,例如:

xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"
xmlns:b="http://www.w3.org/2001/XMLSchema"
Run Code Online (Sandbox Code Playgroud)

这应该在根元素中更好地定义一次,以便可以简单地优化字节.有没有办法将自定义命名空间信息添加到根元素?

这是一个更大的例子来证明我的意思:

<DataObject xmlns="http://schemas.datacontract.org/2004/07/Test"
            xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <Data xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
    <a:KeyValueOfstringanyType>
      <a:Key>ID</a:Key>
      <a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema">1</a:Value>
    </a:KeyValueOfstringanyType>
    <a:KeyValueOfstringanyType>
      <a:Key>Value</a:Key>
      <a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema">42</a:Value>
    </a:KeyValueOfstringanyType>
  </Data>
  <Data xmlns:a="...">...</Data>
  <Data xmlns:a="...">...</Data>
  <Data xmlns:a="...">...</Data>
</DataObject>
Run Code Online (Sandbox Code Playgroud)

我想要的是这样的:

<DataObject xmlns="http://schemas.datacontract.org/2004/07/Test"
            xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"
            xmlns:b="http://www.w3.org/2001/XMLSchema">
  <Data>
    <a:KeyValueOfstringanyType>
      <a:Key>ID</a:Key>
      <a:Value i:type="b:int">1</a:Value>
    </a:KeyValueOfstringanyType>
    <a:KeyValueOfstringanyType>
      <a:Key>Value</a:Key>
      <a:Value i:type="b:int">42</a:Value>
    </a:KeyValueOfstringanyType>
  </Data>
  <Data>...</Data>
  <Data>...</Data>
  <Data>...</Data>
</DataObject>
Run Code Online (Sandbox Code Playgroud)

Mar*_*rot 12

static void Main()
{
    var o = new Foo {
        Prop = new Dictionary<string,string> { {"foo","bar"} }
    };

    var ms = new MemoryStream();

    var slz = new DataContractSerializer(typeof(Foo));
    slz.WriteObject(ms, o,
        new Dictionary<string,string>
        {
            { "arr", "http://schemas.microsoft.com/2003/10/Serialization/Arrays" },
        });

    string data = Encoding.UTF8.GetString(ms.ToArray());
    Console.WriteLine(data);
}

public static class Extensions
{
    public static void WriteObject(
        this DataContractSerializer serializer,
        Stream stream, object data,
        Dictionary<string,string> namespaces)
    {
        using (var writer = XmlWriter.Create(stream))
        {
            serializer.WriteStartObject(writer, data);
            foreach (var pair in namespaces)
            {
                writer.WriteAttributeString("xmlns", pair.Key, null, pair.Value);
            }
            serializer.WriteObjectContent(writer, data);
            serializer.WriteEndObject(writer);
        }
    }
}

[DataContract]
class Foo
{
    [DataMember]
    public Dictionary<string,string> Prop;
}
Run Code Online (Sandbox Code Playgroud)

输出:

<?xml version="1.0" encoding="utf-8"?>
<Foo xmlns:arr="http://schemas.microsoft.com/2003/10/Serialization/Arrays"
     xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://schemas.datacontract.org/2004/07/">
    <Prop>
        <arr:KeyValueOfstringstring>
            <arr:Key>foo</arr:Key>
            <arr:Value>bar</arr:Value>
        </arr:KeyValueOfstringstring>
    </Prop>
</Foo>
Run Code Online (Sandbox Code Playgroud)