代码改变世界

[C#]将一个类序列化为XML

2010-06-04 13:43 by 轩脉刃, ... 阅读, ... 评论, 收藏, 编辑

在C#中要想将一个类序列化为XML,则使用XmlSerializer类:

 

代码
XmlSerializer serializer = new XmlSerializer(typeof(PurchaseOrder));
TextWriter writer
= new StreamWriter(filename);
PurchaseOrder po
=new PurchaseOrder();
// Serialize the purchase order, and close the TextWriter.
serializer.Serialize(writer, po);
writer.Close();

 

可以参考WebService生成代理类的结构