xml和模型对象之间的序列化和反序列化
如 我定义了一个事件信息的模型
[Serializable] public class EventM { public string eventid { get; set; } public string ownertype { get; set; } public string owner { get; set; } public string time { get; set; } public string desp { get; set; } public string handled { get; set; } public string handleduser { get; set; } public string handedtime { get; set; } }
顺便有实例化了一个事件对象
EventM em = new EventM(); em.eventid = "1312"; em.time = DateTime.Now.ToString(); em.owner = "某某人"; em.ownertype = "太帅了"; em.desp = "罚款100元";
无聊 又感觉一个太少了 于是乎
List<EventM> emList1 = new List<EventM>(); for (int i = 0; i < 10; i++) { EventM em = new EventM(); em.eventid = "1312"; em.time = DateTime.Now.ToString(); em.owner = "某某人"; em.ownertype = "太帅了"; em.desp = "罚款100元"; emList1.Add(em); }
下面进行序列化
XmlSerializer serializer = new XmlSerializer(emList1.GetType()); StringWriter writer = new StringWriter(); serializer.Serialize(writer, emList1); writer.Close();
首先顶一个一个序列化对象serializer 然后是一个写入对象writer
开始对emList1序列化serializer.Serialize(writer, emList1); 并写入到writer里 最后要记得 writer.Close();的好习惯
结果
如果吧上段代码中的StringWriter 换成XmlWriter就可以得到一个xml文件 里面的代码为
<?xml version="1.0" encoding="utf-8"?> <ArrayOfEventM xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <EventM> <eventid>1312</eventid> <ownertype>太帅了</ownertype> <owner>某某人</owner> <time>2012/8/27 9:18:33</time> <desp>罚款100元</desp> </EventM> <EventM> <eventid>1312</eventid> <ownertype>太帅了</ownertype> <owner>某某人</owner> <time>2012/8/27 9:18:33</time> <desp>罚款100元</desp> </EventM> <EventM> <eventid>1312</eventid> <ownertype>太帅了</ownertype> <owner>某某人</owner> <time>2012/8/27 9:18:33</time> <desp>罚款100元</desp> </EventM> <EventM> <eventid>1312</eventid> <ownertype>太帅了</ownertype> <owner>某某人</owner> <time>2012/8/27 9:18:33</time> <desp>罚款100元</desp> </EventM> <EventM> <eventid>1312</eventid> <ownertype>太帅了</ownertype> <owner>某某人</owner> <time>2012/8/27 9:18:33</time> <desp>罚款100元</desp> </EventM> <EventM> <eventid>1312</eventid> <ownertype>太帅了</ownertype> <owner>某某人</owner> <time>2012/8/27 9:18:33</time> <desp>罚款100元</desp> </EventM> <EventM> <eventid>1312</eventid> <ownertype>太帅了</ownertype> <owner>某某人</owner> <time>2012/8/27 9:18:33</time> <desp>罚款100元</desp> </EventM> <EventM> <eventid>1312</eventid> <ownertype>太帅了</ownertype> <owner>某某人</owner> <time>2012/8/27 9:18:33</time> <desp>罚款100元</desp> </EventM> <EventM> <eventid>1312</eventid> <ownertype>太帅了</ownertype> <owner>某某人</owner> <time>2012/8/27 9:18:33</time> <desp>罚款100元</desp> </EventM> <EventM> <eventid>1312</eventid> <ownertype>太帅了</ownertype> <owner>某某人</owner> <time>2012/8/27 9:18:33</time> <desp>罚款100元</desp> </EventM> </ArrayOfEventM>
下面是反序列化 我已经生成了一个xml文件xmltest.xml
反序列化代码如下
XmlSerializer serialize = new XmlSerializer(typeof(List<EventM>)); FileStream stream = new FileStream("xmltest.xml", FileMode.Open); List<EventM> emList2 = new List<EventM>(); emList2 = (List<EventM>)serialize.Deserialize(stream); stream.Close();
即可得到反序行列话之后的内容
由于我使用的要求不多 随意用的比较简单基础
还有一篇详细些的博客:
注:XmlSerializer 需要引用命名空间
using System.Xml.Serialization