C# Dictionary的xml序列化
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
namespace SerialDict
{
[Serializable]
public class SerialDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable
{
private string name;
public SerialDictionary()
{
this.name = "SerialDictionary";
}
public SerialDictionary(string name)
{
this.name = name;
}
public void WriteXml(XmlWriter write) // Serializer
{
XmlSerializer KeySerializer = new XmlSerializer(typeof(TKey));
XmlSerializer ValueSerializer = new XmlSerializer(typeof(TValue)); write.WriteAttributeString("name", name); write.WriteStartElement(name);
foreach (KeyValuePair<TKey, TValue> kv in this)
{
write.WriteStartElement("element");
write.WriteStartElement("key");
KeySerializer.Serialize(write, kv.Key);
write.WriteEndElement();
write.WriteStartElement("value");
ValueSerializer.Serialize(write, kv.Value);
write.WriteEndElement();
write.WriteEndElement();
}
write.WriteEndElement();
}
public void ReadXml(XmlReader reader) // Deserializer
{
reader.Read();
XmlSerializer KeySerializer = new XmlSerializer(typeof(TKey));
XmlSerializer ValueSerializer = new XmlSerializer(typeof(TValue)); name = reader.Name;
reader.ReadStartElement(name);
while (reader.NodeType != XmlNodeType.EndElement)
{
reader.ReadStartElement("element");
reader.ReadStartElement("key");
TKey tk = (TKey)KeySerializer.Deserialize(reader);
reader.ReadEndElement();
reader.ReadStartElement("value");
TValue vl = (TValue)ValueSerializer.Deserialize(reader);
reader.ReadEndElement();
reader.ReadEndElement(); this.Add(tk, vl);
reader.MoveToContent();
}
reader.ReadEndElement();
reader.ReadEndElement(); }
public XmlSchema GetSchema()
{
return null;
}
}
class Program
{
static void Main(string[] args)
{
SerialDictionary<string, uint> sd = new SerialDictionary<string, uint>("modelNextIDs");
sd.Add("xxxx", );
sd.Add("xxxxx", );
sd.Add("xxxxxx", );
string fileName = "D:\\s.xml";
using (FileStream fileStream = new FileStream(fileName, FileMode.Create))
{
XmlSerializer xmlFormatter = new XmlSerializer(sd.GetType());
xmlFormatter.Serialize(fileStream, sd);
} SerialDictionary<string, uint> sd2 = new SerialDictionary<string, uint>("modelNextIDs");
using (FileStream fileStream2 = new FileStream(fileName, FileMode.Open))
{
XmlSerializer xmlFormatter = new XmlSerializer(sd2.GetType());
sd2 = xmlFormatter.Deserialize(fileStream2) as SerialDictionary<string, uint>;
}
}
}
}
C# Dictionary的xml序列化的更多相关文章
- Dictionary(支持 XML 序列化),注意C#中原生的Dictionary类是无法进行Xml序列化的
/// <summary> /// Dictionary(支持 XML 序列化) /// </summary> /// <typeparam name="TKe ...
- 支持 XML 序列化的 Dictionary
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.X ...
- .NET中XML序列化的总结
[题外话] 以前虽然常用.NET中的序列化,但是常用的BinaryFormatter,也就是二进制文件的序列化,却鲜用XML的序列化.对于XML序列化,.NET中同样提供了一个非常方便的工具XmlSe ...
- c# XML序列化与反序列化
c# XML序列化与反序列化 原先一直用BinaryFormatter来序列化挺好,可是最近发现在WinCE下是没有办法进行BinaryFormatter操作,很不爽,只能改成了BinaryWrite ...
- Windows phone 之XML序列化与反序列化
为什么要做序列化和反序列化? 一个回答: 我们都知道对象是不能在网络中直接传输的,不过还有补救的办法.XML(Extensible Markup Language)可扩展标记语言,本身就被设计用来存储 ...
- 【C#】【对象转XML】xml序列化
笔记:xml序列化 /// <summary> /// xml序列化 /// </summary> /// <param nam ...
- XML序列化 判断是否是手机 字符操作普通帮助类 验证数据帮助类 IO帮助类 c# Lambda操作类封装 C# -- 使用反射(Reflect)获取dll文件中的类型并调用方法 C# -- 文件的压缩与解压(GZipStream)
XML序列化 #region 序列化 /// <summary> /// XML序列化 /// </summary> /// <param name="ob ...
- 继承自DynamicObject的对象的Xml序列化
默认情况下,对继承自DynamicObject的对象进行序列化操作是不会报错的,但是并没有实际序列化出任何东西来 为了让它进行序列化,我们改造一下实现类,实现IXmlSerializable接口 代码 ...
- C# Json库 和 xml 序列化反序列化 存在的问题
json 正常情况下不会对私有成员进行序列化和反序列化, 因此在用json做深拷贝时, 就会丢失数据. 解决办法: 声明成公有成员. json在序列化和反序列化时, 如果类中有IComma ...
随机推荐
- HTML基础(1) 全局架构标签,特殊字符
最基本的网页文件组成部分 其中 <head></head> 这个标签对中内容不会显示在网页中 <body></body> 中的内容可以显示在网页中. b ...
- MySQL execute dynamic sql script.
SET @sql = (SELECT IF( (SELECT COUNT(*) FROM usher_network_log ) > 1000000, "SELECT 0", ...
- hibernate逆向工程生成的实体映射需要修改
根据实际情况进行修改,主要2处,注释的位置<!-- 把catalog="platform"删掉 -->,<!-- 替换为native --> <? ...
- 自定义Operation
1.要自定义一个Operation 首先要创建一个继承于NSOperation的类. 2.在创建好的类的.h文件声明自定义的方法:-(instancetype)initWithDownLoadMess ...
- Python:文件操作
#!/usr/bin/python3 str1 = input("请输入:") print("你输入的是:",str1) f=open("abc.tx ...
- Java 设计模式_代理模式(2016-08-19)
概念: 代理模式是对象的结构模式.代理模式给某一个对象提供一个代理对象,并由代理对象控制对原对象的引用. 就是一个人或者机构代表另一个人或者机构采取行动.在一些情况下,一个客户不想或者不能够直接引用一 ...
- POJ 1936 All in All(模拟)
All in All 题目链接:http://poj.org/problem?id=1936 题目大意:判断从字符串s2中能否找到子串s1.字符串长度为10W. Sample Input sequen ...
- 343. Integer Break -- Avota
问题描述: Given a positive integer n, break it into the sum of at least two positive integers and maximi ...
- js中的prototye
前言 没事的时候写着js完,一般可能大家都知道这个属性吧,但是我还要说说,给一些不知道的人看看吧, 希望对你有帮助. 过程 以前在学c#的时候,老师最多用的就是Person这个类来开讲,我觉得是这个更 ...
- struts.xml文件示范
<?xml version="1.0" encoding="GBK"?> <!--下面指定Struts2配置文件的DTD信息--> &l ...