C#中XM序列化和反序列.doc_第1页
C#中XM序列化和反序列.doc_第2页
C#中XM序列化和反序列.doc_第3页
C#中XM序列化和反序列.doc_第4页
C#中XM序列化和反序列.doc_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

XML序列与反序列化 XML序列化与反序列化 / OBJECT - XML public static void SaveXml(string filePath, object obj) SaveXml(filePath, obj, obj.GetType(); public static void SaveXml(string filePath, object obj, System.Type type) using (System.IO.StreamWriter writer = new System.IO.StreamWriter(filePath) System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(type); xs.Serialize(writer, obj); writer.Close(); / XML - OBJECT public static object LoadXml(string filePath, System.Type type) if (!System.IO.File.Exists(filePath) return null; using (System.IO.StreamReader reader = new System.IO.StreamReader(filePath) System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(type); object obj = xs.Deserialize(reader); reader.Close(); return obj; 相关的常用Attribute(命名空间System.Xml.Serialization ) XmlRootAttribute(PurchaseOrder, Namespace=/, IsNullable=false) / 指定根 XmlIgnoreAttribute / 跳过不序列化 XmlArrayAttribute(Items) public OrderedItem OrderedItems; / 层次序列化: . XmlElementAttribute(ElementName=Link, IsNullable=false) public Link Links; / 平面序列化: . XmlAttribute(Cat) public string Cat; / 表现为属性 XmlElementAttribute(IsNullable=false) / 表现为节点.相关的全部Attribute(命名空间System.Xml.Serialization ) XmlAttributes 表示一个特性对象的集合,这些对象控制 XmlSerializer 如何序列化和反序列化对象。 XmlArrayAttribute 指定 XmlSerializer 应将特定的类成员序列化为 XML 元素数组。 XmlArrayItemAttribute 指定 XmlSerializer 可以放置在序列化数组中的派生类型。 XmlArrayItemAttributes 表示 XmlArrayItemAttribute 对象的集合。 XmlAttributeAttribute 指定 XmlSerializer 应将类成员作为 XML 特性序列化。 XmlChoiceIdentifierAttribute 指定可以通过使用枚举来进一步消除成员的歧义。 XmlElementAttribute 在 XmlSerializer 序列化或反序列化包含对象时,指示公共字段或属性表示 XML 元素。 XmlElementAttributes 表示 XmlElementAttribute 的集合,XmlSerializer 将其用于它重写序列化类的默认方式。 XmlEnumAttribute 控制 XmlSerializer 如何序列化枚举成员。 XmlIgnoreAttribute 指示 XmlSerializer 的 Serialize 方法不序列化公共字段或公共读/写属性值。 XmlIncludeAttribute 允许 XmlSerializer 在它序列化或反序列化对象时识别类型。 XmlRootAttribute 控制视为 XML 根元素的属性目标的 XML 序列化。 XmlTextAttribute 当序列化或反序列化包含类时,向 XmlSerializer 指示应将此成员作为 XML 文本处理。 XmlTypeAttribute 控制当属性目标由 XmlSerializer 序列化时生成的 XML 架构。 XmlAnyAttributeAttribute 指定成员(返回 XmlAttribute 对象的数组的字段)可以包含任何 XML 属性。 XmlAnyElementAttribute 指定成员(返回 XmlElement 或 XmlNode 对象的数组的字段)可以包含对象,该对象表示在序列化或反序列化的对象中没有相应成员的所有 XML 元素。 XmlAnyElementAttributes 表示 XmlAnyElementAttribute 对象的集合。 XmlAttributeEventArgs 为 UnknownAttribute 事件提供数据。 XmlAttributeOverrides 允许您在使用 XmlSerializer 序列化或反序列化对象时重写属性、字段和类特性。 XmlElementEventArgs 为 UnknownElement 事件提供数据。 XmlNamespaceDeclarationsAttribute 指定目标属性、参数、返回值或类成员包含与 XML 文档中所用命名空间关联的前缀。 XmlNodeEventArgs 为 UnknownNode 事件提供数据。 XmlSerializer 将对象序列化到 XML 文档中和从 XML 文档中反序列化对象。XmlSerializer 使您得以控制如何将对象编码到 XML 中。 XmlSerializerNamespaces 包含 XmlSerializer 用于在 XML 文档实例中生成限定名的 XML 命名空间和前缀。 XmlTypeMapping 包含从一种类型到另一种类型的映射。 xml序列化答疑 (1)需序列化的字段必须是公共的(public) (2)需要序列化的类都必须有一个无参的构造函数 (3)枚举变量可序列化为字符串,无需用XmlInclude (4)导出非基本类型对象,都必须用XmlInclude事先声明。该规则递归作用到子元素 如导出ArrayList对象,若其成员是自定义的,需预包含处理: using System.Xml.Serialization; XmlInclude(typeof(自定义类) (5)Attribute中的IsNullable参数若等于false,表示若元素为null则不显示该元素。 也就是说:针对值类型(如结构体)该功能是实效的 若数组包含了100个空间,填充了10个类对象,则序列化后只显示10个节点 若数组包含了100个空间,填充了10个结构体对象,则序列化后会显示100个节点 (6)真正无法XML序列化的情况 某些类就是无法XML序列化的(即使使用了XmlInclude) IDictionary(如HashTable) System.Drawing.Color System.Drawing.Font SecurityAttribute声明 父类对象赋予子类对象值的情况 对象间循环引用 (7)对于无法XML序列化的对象,可考虑 使用自定义xml序列化(实现IXmlSerializable接口) 实现IDictionary的类,可考虑(1)用其它集合类替代;(2)用类封装之,并提供Add和this函数 某些类型需要先经过转换,然后才能序列化为 XML。如XML序列化System.Drawing.Color,可先用ToArgb()将其转换为整数 过于复杂的对象用xml序列化不便的话,可考虑用二进制序列化 -高级议题-序列化中异常的扑捉 使用Exception.Message只会得到简单的信息“行*错误 可以使用Exception.InnerException.Message得到更详尽的信息可使用事件代理来处理解析不了的XML节点 XmlSerializer serializer = new XmlSerializer(typeof(PurchaseOrder); serializer.UnknownNode+= new XmlNodeEventHandler(serializer_UnknownNode); serializer.UnknownAttribute+= new XmlAttributeEventHandler(serializer_UnknownAttribute); protected void serializer_UnknownNode(object sender, XmlNodeEventArgs e) Console.WriteLine(Unknown Node: + e.Name + t + e.Text); protected void serializer_UnknownAttribute(object sender, XmlAttributeEventArgs e) System.Xml.XmlAttribute attr = e.Attr; Console.WriteLine(Unknown attribute + attr.Name + = + attr.Value + ); 集合类(IEnumerable, ICollection)必须满足下列规则才可XML序列化: - 不得实现 IDictionary。 - 必须有一个 Add 方法,该方法不是由该接口定义的,因为它通常是为该集合将要容纳的专用类型而创建的 - 必须有一个索引器, 且参数为 System.Int32 (C# int) - 在 Add、Count 和索引器中不能有任何安全特性(SecurityAttribute) 可序列化集合类例程: public class PublisherCollection : CollectionBase public int Add(Publisher value) return base.InnerList.Add(value); public Publisher thisint idx get return (Publisher) base.InnerListidx; set base.InnerListidx = value; 某些类是以程序集的形式提供的,无法修改其源码。可用XmlAttributeOverrides设置其序列化特性 XML目标 100 Product Thing 10 101 How to Use Your New Product Thing 10 123456789 源类(无法修改) public class Inventory private Product stuff; public Inventory() public Product InventoryItems get return stuff; set stuff=value; 附加XmlAttributeOverrides后即可序列化 XmlAttributes attrs = new XmlAttributes(); attrs.XmlElements.Add(new XmlElementAttribute(Book, typeof(BookProduct); attrs.XmlElements.Add(new XmlElementAttribute(Product, typeof(Product); /add to the Attributes collection XmlAttributeOverrides attrOver = new XmlAttributeOverrides(); attrOver.Add(typeof(Inventory), InventoryItems, attrs); /deserialize and load data into the listbox from deserialized object FileStream f=new FileStream(.inventory.xml,FileMode.Open); XmlSerializer newSr=new XmlSerializer(typeof(Inventory), attrOver); Inventory newInv = (Inventory)newSr.Deserialize(f);-最简单的示例-类设计 public class MyClass public MyObject MyObjectProperty; public class MyObject public string ObjectName;序列化的 XML: My String -示例: 序列化数组,并限制数组元素类型-类设计 public class Things XmlElement(DataType = typeof(string), XmlElement(DataType = typeof(int) public object StringsAndInts; 生成的 XML 可能为: Hello 999 World -示例: 序列化数组-类设计 using System.Xml.Serialization; XmlRootAttribute(LinkLibrary, IsNullable = false, Namespace=/) public class LinkLib XmlElementAttribute(ElementName=Link, IsNullable=false) public Link Links; public LinkLib() Links = new Link50; Links0 = new Link(aa, aa, aa); Links1 = new Link(bb, aa, aa); Links2 = new Link(cc, aa, aa); Links3 = new Link(aa, aa, aa); Links4 = new Link(aa, aa, aa); Links5 = new Link(aa, aa, aa); Links6 = new Link(aa, aa, aa); Links7 = new Link(aa, aa, aa); Links8 = new Link(aa, aa, aa); Links9 = new Link(aa, aa, aa); public class Link XmlAttribute(Cat) public string Cat; XmlAttribute(Url) public string Url; XmlAttribute(Desc)public string Desc; public Link() public Link(string cat, string url, string desc) Cat = cat; Url = url; Desc = desc; 目标XML文件 若使用XmlArrayAttribute(Links) public Link Links;则序列化后的xml文件会多出一层: -示例:使用自定义序列化序列化Dictionary对象-XML目标 FactTableDef1 owner1 sourceTable1 类源码 using System; using System.Collections.Generic; using System.Text; using System.Runtime.Serialization; using System.Xml; using System.Xml.Serialization; namespace WZDM.OLAP System.Serializable() XmlInclude(typeof(FactTableDef) public class FactTableDef : System.Xml.Serialization.IXmlSerializable public string Name; / 名称 public string Owner; / 事实表属主 public string SourceTable; / 源表 public Dictionary ColumnMeasureMaps; / 字段和量度对应关系 public Dictionary ColumnDimensionMaps; / 字段和维度对应关系 public FactTableDef() . public void WriteXml(System.Xml.XmlWriter writer) writer.WriteElementString(Name, this.Name); writer.WriteElementString(Owner, this.Owner); writer.WriteElementString(SourceTable, this.SourceTable); / ColumnMeasureMaps writer.WriteStartElement(ColumnMeasureMaps); foreach (string key in this.ColumnMeasureMaps.Keys) writer.WriteStartElement(Map); writer.WriteAttributeString(Column, key); writer.WriteAttributeString(Measure, ColumnMeasureMapskey); writer.WriteEndElement(); writer.WriteEndElement(); / ColumnDimensionMaps writer.WriteStartElement(ColumnDimensionMaps); foreach (string key in this.ColumnDimensionMaps.Keys) writer.WriteStartElement(Map); writer.WriteAttributeString(Column, key); writer.WriteAttributeString(Dimension, ColumnDimensionMapskey); writer.WriteEndElement(); writer.WriteEndElement(); public void ReadXml(System.X

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论