.net内置了串行化支持,在System.xml.Serialization中提供了一个串行化器--SeriLizer.
SeriLizer serilizer=new SeriliZer()
有下面几种串行化方式:
1、简单串行化方式。
在这种方式下,定义的类要用 .net的属性或特性来标注类公有属性或字段。
[XmlRootAttribute()]/[XmlElementAttribute()]/[XmlAttributeAttribute()].....
串行化步骤:
a、建立流对象
b、建立串行化器
c、调用serilize()方法。
d、关闭流对象。
注意类中属性返回数组的情况,要用XmlArrayItem('ss',typeof(custum_type))属性标注该属性
2、复杂串行化。
当不能访问类的源代码时,可以动态的添加特性。
利用XmlAttributes 和 XmlAttributeOverrides类。
XmlAttributes attrs=new XmlAttributes ();
attrs.XmlElements.add(new XmlElementAttribute('Book',typeof(BookProduct)));
attrs.xmlElements.add(new XmlElementAttribute(,) )
//-------------------------------------------------------------------------------------以上是建立属性/特性集
XmlAttributeOverrides overide=new XmlAttributeOverrides();
overide.add(typeof(串行化类名),‘属性关联成员名’,attrs);
串行化步骤:........