《c#XML序列化与反序列化.docx》由会员分享,可在线阅读,更多相关《c#XML序列化与反序列化.docx(164页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、C# XML序列化与反序列化很不爽,只能改成了 Binary Writer和 Binary Reader来读写,突然想到能不 能用XML来序列化?于是在网上查 了些资料便写了些实践性代码,做些记录,避免以后忘记。序列化对象public class PeopleXmlAttribute(nNAMEn)public string Name set; get; XmlAttribute(nAGEn)public int Age set; get; XmlRoot(nRootn)public class Student: PeopleXmlElement(n CLAS Sn)public string
2、 Class set; get; XmlElement(nNUMBERn)public int Number set; get; void Main(string args)Student stu = new Student()rClass 二Class OneName = TomNumber = 1XmlSerializer ser = new XmlSerializer(typeof(Student);ser.Serialize(File.Create(HC:x.xml” stu);反序列化对象XmlSerializer ser 二 newXmlSerializer(typeof(Stud
3、ent);Student stu =ser.Deserialize(File.OpenRead(HC:x.xm)as Student;对象数组序列化public class PeopleXmlAttribute(nNAMEn)public string Name set; get; XmlAttribute(nAGEn)public int Age set; get; XmlRoot(nRootn)public class Student: PeopleXmlElement(n CLAS Sn)public string Class set; get; XmlElement(nNUMBERn)
4、public int Number set; get; void Main(string args)List stuList new List();stuList.Add(new Student() Age = 10, Number = 1, Name = ”Tom Class 二 Class Onen );stuList.Add(new Student() Age = 11, Number = 2, Name = 叮ay,Class 二 Class Two );stuList.Add(new Student() Age = 12, Number = 3, Name = ”Pet Class
5、二 Class Onen );stuList.Add(new Student() Age = 13, Number = 4, Name = “May,Class = nClass Threen );stuList.Add(new Student() Age = 14, Number = 5, Name = “Soy,Class 二 Class Two );XmlSerializer ser = newXmlSerializer(typeof(List);ser.Serialize(File.Create(HC:x.xml,stuList);对象数组反序列XmlSerializer ser =
6、newXmlSerializer(typeof(List);List stuList =ser.Deserialize(File.OpenRead(HC:x.xm)as List;fOreach (Student s in stuList)MessageBox.Show(string.Format(n。:1:2:3s.Name, s.Age, s.Class, s.Number);序列化 Dirctionarypublic struct DirectionListXmlAttribute ( N amen)public string Name;XmlElement(n Valuen)publi
7、c int Value;void Main(string args)Dictionary list =new Dictionary();list.Add(nr, 100);list.AddC2 200);list.Add(n3n, 300);list.Add(n4n, 400);hstAddCS11, 500);list.Add(n6n, 600);list.Add(n7n, 700);,(008 :Jppvisrlist.AddC9 900);List dirList new List();foreach (var s in list)dirList.Add(newDirectionList
8、() Name = s.Key,Value = s.Value );XmlSerializer ser 二 newXmlSerializer(typeof(List);ser.Serialize(File.Create(nC:x.xmln), dirList);1-1-X / Q II II H 、1= T。L 4 I 、只能按照它的格式先创建一个可 以别序列化的类型,这里我定义 了一个结构体,当然你也可以定 义成其他的类。将Dictionaryv 中的数据依次放进结构体以后就 可以放入流中了。XmlAttribute (N ame 意思是将这个字段作为xml的属性,属性名跟在中XmlElem
9、ent( Value)意思是将 这个字段做为xml的元素。反序列化DirctionaryXmlSerializer ser 二 newXmlSerializer(typeof(List);List dirList =ser.Deserialize(File.OpenRead(nC:x.xmln) asList;foreach (var v in dirList)Console.WriteLine(n0: 1 f v.Name, v.Value);是这样了,没有太炫的地方,Deserialize反序列化。真希望.Net能集成Dirctionaryv对象,那我们这些懒人就方便了。在需要序列化的队伍
10、中,数组是 很常见的类型,其次就是图片了序列化图片public struct ImageStructXmlAttribute ( N umbern)public int number;XmlElement (f f Imagef f)public byte picture;void Main(string args)ImageStruct() number = 1,picture =File.ReadAllBytes( H.jpg);XmlSenalizer ser = newXmlSerializer(typeof(ImageStruct);FileStream fs =File.Creat
11、e(nc:x.xmr);fs.CloseQ;样的,采用结构体来保存图片,这里我还加了个图片的名字,到时候查找起来也方便些图片反序列化XmlSenalizer ser = newXmlSerializer(typeof(ImageStruct);ImageStruct s =(ImageStruct)ser.Deserialize(File.OpenRead(nc:x.xmln);pictureBox 1 .Image 二 Image.FromStream(new Memory Stream(s .picture);memorystream来做缓存,这样会 比较快点,实际上我并没有怎 么感觉。图
12、片数组序列化public struct ImageStructXmlAttribute ( N umbern)public int number;XmlElement (f f Imagef f)public byte picture;void Main(string args)List imageList = new Li s t ();imageList.Add(newImageStruct()number =1,picture = File.ReadAllBytes( ” 11 .jpg”) cimageList.Add(newImageStruct()picture =File.Rea
13、dAllBytes( n22.jpgn) cXmlSerializer ser 二 newXmlSerializer(typeof(List);FileStream fs =File.Create(nc:x.xmr);ser.Serialize(fs, imageList);fs.CloseQ;图片数组反序列化XmlSerializer ser 二 newXmlSerializer(typeof(List);List s = (List)ser.Deserialize (File.OpenRead(c:x.xm );var im = from i in swhere i.number = 1s
14、elect i.picture;/var im = s.Where(p = p. number = l).Select(p =p.picture);foreach (var image in im)pictureB ox 1. Image = Image. FromS tre am(newMemory Stream(image);这里还对数组结构进仃Linq查 询,这样就可以很方便的查询图 片了。要序列化的对象的类:Serializablepublic class Personprivate string name;public string Namereturn name;name=value;