DataContract中字典的自定义序列化 [英] Custom serialisation of a Dictionary in a DataContract

查看:89
本文介绍了DataContract中字典的自定义序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我有一个DataContract,其中包含Dictionary:

Basically I have a DataContract which contains a Dictionary:

[DataContract]
public class MyDictionary : IDictionary<string, object> {
    [DataMember(Name = "Data")]
    protected IDictionary<string, object> Dictionary { get; private set; }

    // ...
}

这是XML输出的相关部分:

Here is the relevent part of the XML output:

<Data>
 <a:KeyValueOfstringanyType>
  <a:Key>ID</a:Key>
  <a:Value i:type="s:int">2</a:Value>
 </a:KeyValueOfstringanyType>
 <a:KeyValueOfstringanyType>
  <a:Key>Value</a:Key>
  <a:Value i:type="s:int">4711</a:Value>
 </a:KeyValueOfstringanyType>
</Data>

如何将输出简化为如下所示:

How can I simplify the output to something like this here:

<Data>
  <ID i:type="s:int">2</ID>
  <Value i:type="s:int">4711</Value>
</Data>

Dictionary键仅限于字符串,因此,如果没有人想到使用应该正常工作的non ascii键的愚蠢想法.我发现属性CollectionDataContract与我想要的属性更加接近,但是键值对将被保存起来,从而浪费了内存.也许可以嘲笑类ISerializable,但是我不确定DataContractSerializer是否会带来一些麻烦.顺便说一句,该解决方案也应与DataContractJsonSerializer一起使用.

The Dictionary key is restricted to string so if nobody get the stupid idea of using non ascii keys that should work fine. I found the attribute CollectionDataContract with that I come a little bit closer to what I want but the key value pairs will be saved compleate which wasts memory. Maybe it is possible to slove with the class ISerializable but I'm not sure if that makes some trouble with the DataContractSerializer. By the way the solution should also work with the DataContractJsonSerializer.

推荐答案

您遇到的问题是由于IDictionary<'string,object>是IEnumerable<'KeyValuePair<'string,object> >,这就是DataContractSerializer序列化每个KeyValuePair个性的方式.

The problem you are having comes from the fact that IDictionary<'string, object> is (in a way) IEnumerable<'KeyValuePair<'string, object>>, and that is way the DataContractSerializer serialize each KeyValuePair individuality.

您要问的是(如果我理解正确的话)是创建自定义序列化,为此,您可以实现

What you are asking (if I understand correctly) is to create a custom serialization, and for that you can implement the IXmlSerializable interface.

使用WriteXml和ReadXml函数控制XmlWriter作为参数传递到流中的xml.

Use the WriteXml, and ReadXml functions to control the xml written to the stream with the XmlWriter passed as parameter.

例如此功能

public void WriteXml(XmlWriter writer)
    {
        foreach (var pair in _dictionary)
        {
            writer.WriteElementString("Key", pair.Key);
            writer.WriteElementString("Value", pair.Value.ToString());
        }
    }

将产生此结果

<MyDictionary xmlns="http://schemas.datacontract.org/2004/07/Sandbox">
    <Key>ID</Key>
    <Value>15</Value>
    <Key>Value</Key>
    <Value>123</Value>
</MyDictionary>

假设已将两对输入到字典中(ID,15和值,123).

assuming two pairs has been entered to the dictionary (ID,15 & Value, 123).

哦,关于JSON,有一个 IJsonSerializable ,但是我从来没有绕过它.

Oh and about the JSON, there is an IJsonSerializable but I never got around to working with it.

这篇关于DataContract中字典的自定义序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆