欢迎来到天天文库
浏览记录
ID:6579380
大小:61.00 KB
页数:11页
时间:2018-01-18
《xml序列化与反序列化 - 整理文档》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、XML序列化与反序列化整理文档XML序列化与反序列化 //OBJECT->XML publicstaticvoidSaveXml(stringfilePath,objectobj){SaveXml(filePath,obj,obj.GetType());} publicstaticvoidSaveXml(stringfilePath,objectobj,System.Typetype) { using(System.IO.StreamWriterwriter=newSystem.IO.
2、StreamWriter(filePath)) { System.Xml.Serialization.XmlSerializerxs=newSystem.Xml.Serialization.XmlSerializer(type); xs.Serialize(writer,obj); writer.Close(); } } //XML->OBJECT publicstaticobjectLoadXml(stringfil
3、ePath,System.Typetype) { if(!System.IO.File.Exists(filePath)) returnnull; using(System.IO.StreamReaderreader=newSystem.IO.StreamReader(filePath)) { System.Xml.Serialization.XmlSerializerxs=newSystem.Xml.Serialization.Xm
4、lSerializer(type); objectobj=xs.Deserialize(reader); reader.Close(); returnobj; } }相关的常用Attribute(命名空间System.Xml.Serialization) [XmlRootAttribute("PurchaseOrder",Namespace="http://www.cpandl.com/",IsNullable=false)] //指
5、定根 [XmlIgnoreAttribute] //跳过不序列化 [XmlArrayAttribute("Items")]publicOrderedItem[]OrderedItems; //层次序列化:.. [XmlEl
6、ementAttribute(ElementName="Link",IsNullable=false)]publicLink[]Links; //平面序列化:... [XmlAttribute("Cat")]publicstringCat; //表现为属性<...Cat=../> [XmlElementAttribute(IsNullable=false
7、)] //表现为节点..相关的全部Attribute(命名空间System.Xml.Serialization) XmlAttributes 表示一个特性对象的集合,这些对象控制XmlSerializer如何序列化和反序列化对象。 XmlArrayAttribute 指定XmlSerializer应将特定的类成员序列化为
8、XML元素数组。 XmlArrayItemAttribute 指定XmlSerializer可以放置在序列化数组中的派生类型。 XmlArrayItemAttributes 表示XmlArrayItemAttribute对象的集合。 XmlAttributeAttribute 指定XmlSeria
此文档下载收益归作者所有