首页 > 其他分享 >XML Serializable Generic Dictionary

XML Serializable Generic Dictionary

时间:2022-10-24 14:00:10浏览次数:101  
标签:XML Generic writer Serializable key reader XmlSerializer public SerializableDict

    .net 2.0 泛型Dictionary不支持 XML serializable.  下面是一个实现IXmlSerializable 接口实现支持Serialize的泛型集合.Dictionary 。 Dictionary<TKey, TValue>本身实现了ISerializable接口,WebService中无法实现序列化,具体是什么原因它不支持XML序列化。
  

1
/**//// <summary>
2
/// 支持XML序列化的泛型 Dictionary
3
/// </summary>
4
/// <typeparam name="TKey"></typeparam>
5
/// <typeparam name="TValue"></typeparam>
6
[XmlRoot("dictionary")]
7
public class SerializableDictionary<TKey, TValue>
8
: Dictionary<TKey, TValue>, IXmlSerializable
9

{
10
#region 构造函数
public SerializableDictionary():base()
{ }

public SerializableDictionary(IDictionary<TKey, TValue> dictionary):base(dictionary)
{ }


public SerializableDictionary(IEqualityComparer<TKey> comparer):base(comparer)
{
}
public SerializableDictionary(int capacity):base(capacity)
{ }

public SerializableDictionary(int capacity, IEqualityComparer<TKey> comparer)
:base(capacity,comparer)
{ }
protected SerializableDictionary(SerializationInfo info, StreamingContext context):base(info,context)
{ }
#endregion
11
IXmlSerializable Members#region IXmlSerializable Members
12
public System.Xml.Schema.XmlSchema GetSchema()
13

{
14
return null;
15
}
16

17
/**//// <summary>
18
/// 从对象的 XML 表示形式生成该对象
19
/// </summary>
20
/// <param name="reader"></param>
21
public void ReadXml(System.Xml.XmlReader reader)
22

{
23

24
XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
25
XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
26
bool wasEmpty = reader.IsEmptyElement;
27
reader.Read();
28

29
if (wasEmpty)
30
return;
31
while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
32

{
33
reader.ReadStartElement("item");
34
reader.ReadStartElement("key");
35
TKey key = (TKey)keySerializer.Deserialize(reader);
36
reader.ReadEndElement();
37
reader.ReadStartElement("value");
38
TValue value = (TValue)valueSerializer.Deserialize(reader);
39
reader.ReadEndElement();
40
this.Add(key, value);
41
reader.ReadEndElement();
42
reader.MoveToContent();
43

44
}
45
reader.ReadEndElement();
46

47
}
48

49

50
/**//// <summary>
51
/// 将对象转换为其 XML 表示形式
52
/// </summary>
53
/// <param name="writer"></param>
54
public void WriteXml(System.Xml.XmlWriter writer)
55

{
56

57
XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
58
XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
59
foreach (TKey key in this.Keys)
60

{
61
writer.WriteStartElement("item");
62
writer.WriteStartElement("key");
63
keySerializer.Serialize(writer, key);
64
writer.WriteEndElement();
65
writer.WriteStartElement("value");
66
TValue value = this[key];
67
valueSerializer.Serialize(writer, value);
68
writer.WriteEndElement();
69
writer.WriteEndElement();
70
}
71

72
}
73

74
#endregion
75

76
}

标签:XML,Generic,writer,Serializable,key,reader,XmlSerializer,public,SerializableDict
From: https://blog.51cto.com/shanyou/5789600

相关文章

  • 【XML】Java创建XML文档
    packageexample01;importorg.w3c.dom.Document;importorg.w3c.dom.Element;importjavax.xml.parsers.DocumentBuilder;importjavax.xml.parsers.DocumentBuilde......
  • SpringBoot项目整合Mybatis时Mapper.xml文件的存放位置
    SpringBoot项目整合Mybatis时Mapper.xml文件的存放位置目录:方式一:放在与Mapper接口同级目录方式二:在resources创建Mapper接口同名文件夹用来存放Mapper.xml文件方式三:......
  • GenericAPIView
    https://www.bilibili.com/video/BV1z5411D7BQ?p=14&vd_source=caabcbd2a759a67e2a3de8acbaaf08ea views.pyfromsers.modelsimportBookfromrest_frameworkimpor......
  • Spring的@Configiration和@ComponentScan注解代替xml配置文件
    1.使用xml配置文件配置Spring容器:<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:context="http:/......
  • C# HTTP POST AND GET json or xml
    usingSystem.Net;usingSystem.Net.Cache;usingSystem.IO;stringHttpPost(stringstrUrl,stringstrPostData){s......
  • Spring的autoware自动装配,xml版
    1.BookDaoImpl.class这个类的对象将会被自动装配注入到BookServiceImpl类里面。BookServiceImpl类里面讲调用BookDaoImpl类的方法。packagecom.oxygen.dao.impl;imp......
  • java---泛型(Generics)
    泛型是JDK1.5以后增加的,它可以帮助我们建立类型安全的集合。什么是泛型泛型的本质就是“数据类型的参数化”,处理的数据类型不是固定的,而是可以作为参数传入,可以把“泛......
  • xml 解析技术介绍和解析xml文件
     xml解析技术介绍xml可扩展的标记语言。不管是html文件还是xml文件它们都是标记型文档,都可以使用w3c组织制定的dom技术来解析。document对象表示的是整个文档(......
  • 下载插件失败? pom.xml
    parent标签下加,表示直接去远程仓库下载<relativePath/>比如<parent><artifactId>spring-boot-dependencies</artifactId><groupId>org.springfra......
  • implements Serializable
    一开始看ssm代码的时候,看到publicclassAccountimplementsSerializable{这个,不知道这个接口是干嘛的后来去了解了一下,发现...其实没什么卵用,hhhSerializable是......