预定义DataContractSerializer的XML名称空间 [英] Predefine XML namespaces for DataContractSerializer
问题描述
我正在构建一个自托管的WCF服务.我正在构建一种特殊的数据结构,以实现非常灵活的数据传输.到目前为止,我测试了我的结构是否可以使用DataContractSerializer进行序列化.效果很好,对此我感到很高兴,但是有一些令人讨厌的事情:
I'm building a self hosted WCF service. I'm building a special data structure for a very flexible transport of data. So far I test if my structure is serializable using the DataContractSerializer. That works fine and I'm happy about that, but there is something annoying me:
在我的XML输出中,有几十个重新定义的xmlns属性,例如:
In my XML output are dozens redefined xmlns attributes e.g.:
xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"
xmlns:b="http://www.w3.org/2001/XMLSchema"
应该更好地在根元素中定义一次,以便可以简单地优化字节.有没有办法将自定义名称空间信息添加到根元素?
This should be better defined once in the root element so that bytes could be simply optimized. Is there a way to add custom namespace informations to the root element?
这是一个更大的例子来说明我的意思:
Here is a bigger example to demonstrate what I mean:
<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>
我想要的是这样的
<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>
推荐答案
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;
}
输出:
<?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>
这篇关于预定义DataContractSerializer的XML名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!