c# XML序列化与反序列化
c# XML序列化与反序列化
原先一直用BinaryFormatter来序列化挺好,可是最近发现在WinCE下是没有办法进行BinaryFormatter操作,很不爽,只能改成了BinaryWriter和BinaryReader来读写,突然想到能不能用XML来序列化?于是在网上查了些资料便写了些实践性代码,做些记录,避免以后忘记。
序列化对象
public class People
{
[XmlAttribute("NAME")]
public string Name { set; get; }
[XmlAttribute("AGE")]
public int Age { set; get; }
}
[XmlRoot("Root")]
public class Student : People
{
[XmlElement("CLASS")]
public string Class { set; get; }
[XmlElement("NUMBER")]
public int Number { set; get; }
}
void Main(string[] args)
{
Student stu = new Student()
{
Age = 10,
Class = "Class One",
Name = "Tom",
Number = 1
};
XmlSerializer ser = new XmlSerializer(typeof(Student));
ser.Serialize(File.Create("C:\\x.xml"), stu);
}
反序列化对象
XmlSerializer ser = new XmlSerializer(typeof(Student));
Student stu = ser.Deserialize(File.OpenRead("C:\\x.xml")) as Student;
对象数组序列化
public class People
{
[XmlAttribute("NAME")]
public string Name { set; get; }
[XmlAttribute("AGE")]
public int Age { set; get; } }
[XmlRoot("Root")]
public class Student : People
{
[XmlElement("CLASS")]
public string Class { set; get; }
[XmlElement("NUMBER")]
public int Number { set; get; } }
void Main(string[] args)
{
List<Student> stuList = new List<Student>();
stuList.Add(new Student() { Age = 10, Number = 1, Name = "Tom", Class = "Class One" });
stuList.Add(new Student() { Age = 11, Number = 2, Name = "Jay", Class = "Class Two" });
stuList.Add(new Student() { Age = 12, Number = 3, Name = "Pet", Class = "Class One" });
stuList.Add(new Student() { Age = 13, Number = 4, Name = "May", Class = "Class Three" });
stuList.Add(new Student() { Age = 14, Number = 5, Name = "Soy", Class = "Class Two" });
XmlSerializer ser = new XmlSerializer(typeof(List<Student>));
ser.Serialize(File.Create("C:\\x.xml"), stuList);
}
对象数组反序列
XmlSerializer ser = new XmlSerializer(typeof(List<Student>));
List<Student> stuList = ser.Deserialize(File.OpenRead("C:\\x.xml")) as List<Student>;
foreach (Student s in stuList)
{
MessageBox.Show(string.Format("{0} : {1} : {2} : {3}",
s.Name, s.Age, s.Class, s.Number)); }
序列化Dirctionary
public struct DirectionList
{
[XmlAttribute("Name")]
public string Name;
[XmlElement("Value")]
public int Value; }
void Main(string[] args)
{
Dictionary<string, int> list = new Dictionary<string, int>();
list.Add("1", 100);
list.Add("2", 200);
list.Add("3", 300);
list.Add("4", 400);
list.Add("5", 500);
list.Add("6", 600);
list.Add("7", 700);
list.Add("8", 800);
list.Add("9", 900);
List<DirectionList> dirList = new List<DirectionList>();
foreach (var s in list)
{
dirList.Add(new DirectionList() { Name = s.Key, Value = s.Value });
}
XmlSerializer ser = new XmlSerializer(typeof(List<DirectionList>));
ser.Serialize(File.Create("C:\\x.xml"), dirList);
}
这里还要讲一点,在XmlSerializer中,不支持Dirctionary<>类型的对象,所以在序列化这种最常见类型的时候,只能按照它的格式先创建一个可以别序列化的类型,这里我定义了一个结构体,当然你也可以定义成其他的类。将Dictionary<>中的数据依次放进结构体以后就可以放入流中了。
[XmlAttribute("Name")]意思是将这个字段作为xml的属性,属性名跟在“”中
[XmlElement("Value")]意思是将这个字段做为xml的元素。
反序列化Dirctionary
XmlSerializer ser = new XmlSerializer(typeof(List<DirectionList>));
List<DirectionList> dirList = ser.Deserialize(
File.OpenRead("C:\\x.xml")) as List<DirectionList>;
foreach (var v in dirList)
{
Console.WriteLine("{0} : {1}", v.Name, v.Value);
}
其实我并不喜欢这个名称,感觉有点生化危机的feel,但是也就是这样了,没有太炫的地方,Deserialize反序列化。真希望.Net能集成Dirctionary<>对象,那我们这些懒人就方便了。
在需要序列化的队伍中,数组是很常见的类型,其次就是图片了
序列化图片
public struct ImageStruct
{
[XmlAttribute("Number")]
public int number;
[XmlElement("Image")]
public byte[] picture; }
void Main(string[] args)
{
ImageStruct s = new ImageStruct() { number = 1, picture = File.ReadAllBytes(@"11.jpg") };
XmlSerializer ser = new XmlSerializer(typeof(ImageStruct));
FileStream fs = File.Create("c:\\x.xml");
ser.Serialize(fs, s);
fs.Close();
}
一样的,采用结构体来保存图片,这里我还加了个图片的名字,到时候查找起来也方便一些
图片反序列化
XmlSerializer ser = new XmlSerializer(typeof(ImageStruct));
ImageStruct s = (ImageStruct)ser.Deserialize(File.OpenRead("c:\\x.xml"));
pictureBox1.Image = Image.FromStream(new MemoryStream(s.picture));
没有花头的方式,利用memorystream来做缓存,这样会比较快一点,实际上我并没有怎么感觉。
图片数组序列化
public struct ImageStruct
{
[XmlAttribute("Number")]
public int number;
[XmlElement("Image")]
public byte[] picture; }
void Main(string[] args)
{
List<ImageStruct> imageList = new List<ImageStruct>();
imageList.Add(new ImageStruct()
{
number = 1,
picture = File.ReadAllBytes(@"11.jpg")
});
imageList.Add(new ImageStruct()
{
number = 2,
picture = File.ReadAllBytes(@"22.jpg")
});
XmlSerializer ser = new XmlSerializer(typeof(List<ImageStruct>));
FileStream fs = File.Create("c:\\x.xml");
ser.Serialize(fs, imageList);
fs.Close();
}
图片数组反序列化
XmlSerializer ser = new XmlSerializer(typeof(List<ImageStruct>));
List<ImageStruct> s = (List<ImageStruct>)ser.Deserialize(File.OpenRead("c:\\x.xml"));
var im = from i in s
where i.number == 1 select i.picture;
//var im = s.Where(p => p.number == 1).Select(p => p.picture);
foreach (var image in im)
{
pictureBox1.Image = Image.FromStream(new MemoryStream(image));
}
这里还对数组结构进行了Linq查询,这样就可以很方便的查询图片了。
c# XML序列化与反序列化的更多相关文章
- XML 序列化与反序列化
XML序列化与反序列化 1.将一个类转化为XML文件 /// <summary> /// 对象序列化成XML文件 /// </summary> /// <param na ...
- XmlSerializer 对象的Xml序列化和反序列化
http://www.cnblogs.com/yukaizhao/archive/2011/07/22/xml-serialization.html 这篇随笔对应的.Net命名空间是System.Xm ...
- C#的XML序列化及反序列化
webservice在工作中用到的很多,基本都是以XML格式问通讯内容,其中最关键的就是XML串的序列化及反序列化. XML的运用中有两种信息传递,一种为XML的请求信息,另一种为返回信息,要运用XM ...
- .NET XML序列化与反序列化
闲着没事,写了两个通用的XML序列化与反序列化的方法. 贴出来当作笔记吧! /// <summary> /// XML序列化 /// </summary> /// <ty ...
- XmlSerializer 对象的Xml序列化和反序列化,XMLROOT别名设置
这篇随笔对应的.Net命名空间是System.Xml.Serialization:文中的示例代码需要引用这个命名空间. 为什么要做序列化和反序列化? .Net程序执行时,对象都驻留在内存中:内存中 ...
- Xml序列化、反序列化帮助类
之前从网络上找了一个Xml处理帮助类,并整理了一下,这个帮助类针对Object类型进行序列化和反序列化,而不需要提前定义Xml的结构,把它放在这儿供以后使用 /// <summary> / ...
- Windows phone 之XML序列化与反序列化
为什么要做序列化和反序列化? 一个回答: 我们都知道对象是不能在网络中直接传输的,不过还有补救的办法.XML(Extensible Markup Language)可扩展标记语言,本身就被设计用来存储 ...
- C#操作Xml:XmlSerializer 对象的Xml序列化和反序列化
这篇随笔对应的.Net命名空间是System.Xml.Serialization:文中的示例代码需要引用这个命名空间. 为什么要做序列化和反序列化? .Net程序执行时,对象都驻留在内存中:内存中的对 ...
- C#实现接口xml序列化与反序列化
C#实现接口xml序列化与反序列化 C#中接口无法被xml序列化,提示不支持.百度和bing也搜不到,只好自己动手写了 原理上肯定支持,.Net自己的xml序列化有一个IXmlSerializab ...
随机推荐
- 【Android】 Android-wifi 直连 wifi direct wifi p2p
现在,Android的支持Wi -Fi的直接点对点点对点(P2P)Android系统的供电设备和其他类型的设备,没有一个热点或互联网连接之间的连接.Android框架提供了一套Wi - Fi的P2P的 ...
- 安装配置NFS服务
超级好的配置centos下服务的链接 http://www.server-world.info/en/note?os=CentOS_6&p=nfs http://linux.vbird.org ...
- 关闭/开启 ubuntu 自动更新提示
发现vps登陆后只有apt update后才知道有多少包需要更新不是很傻么,本地的ubuntu在登录时就有很好的提示,并且还能告知系统负载情况,很有用,这里就想开起来.首先这个提示的名字叫Motd. ...
- JAX-RS(基于Jersey) + Spring 4.x + MyBatis构建REST服务架构
0. 大背景 众所周知,REST架构已经成为现代服务端的趋势. 很多公司,已经采用REST作为App, H5以及其它客户端的服务端架构. 1. 什么是JAX-RS? JAX-RS是JAVA EE6 引 ...
- CentOS 伪装安装TSA for DB2
DB2 HADR需要额外安装TSA,正常情况下CentOS无法通过安装前验证.会报一个说发行版不支持的错误. 可以通过伪装成RHEL的方式使得正常安装. 修改方式如下 修改/etc/system-re ...
- .NET 4.6中的性能改进
.NET 4.6中带来了一些与性能改进相关的CLR特性,这些特性中有一部分将会自动生效,而另外一些特性,例如SIMD与异步本地存储(Async Local Storage)则需要对编写应用的方式进行某 ...
- 使用Ant编译提示Class not found: javac1.8
无论是使用Eclipse还是使用Ant命令,都可能会在编译时遇到提示:Class not found: javac1.8 今天用Ant打包Android,apk,运行出现了batch_build.xm ...
- Asp.net Core 1.0.1升级到Asp.net Core 1.1.0 Preview版本发布到Windows Server2008 R2 IIS中的各种坑
Asp.net Core 1.0.1升级到Asp.net Core 1.1.0后,程序无法运行了 解决方案:在project.json中加入runtime节点 "runtimes" ...
- windows 应用商店应用笔记
xaml http://www.cnblogs.com/free722/archive/2011/11/06/2238073.html win8 http://blog.csdn.net/ygzk12 ...
- chrome start.js报错
是由 chrome 插件 “电脑管家广告过滤” 引起的 并且,在用户电脑上还出现了这个插件拦截正常请求的情况 如果同时报以下错误: Uncaught TypeError: Cannot read pr ...