刚接触.net时,就看到过一些用到Xml Serialization的例子,当时没什么感觉。后来看了SDK和一些较大的程序后,逐渐有了点感觉。我个人感觉Xml Serialization 的最大好处是可以将一个xml文件对象化,比如说xml中的元素、属性等都可以对应到对象、对象的属性。这样我们可以用对象的思想来操作数据,由于目前主流数据库还只是关系型的(Oracle也只是部分面向对象),我们在数据层的操作显得于其他层面有点隔。而Xml Serialization给了我们这样一个用面向对象的思维来操作数据的可能。据个例子来说,比如说我们要做一个制作工作流程的程序,每个流程有n个阶段,每个阶段有n个人来完成。如果用数据库做,就不可避免的涉及到多表关联。这对于这样一个数据量比较小的程序来说是一个浪费。我们来看一下怎么利用xml来做。
///示例xml文件 <?xml version="1.0"?> <WorkflowData xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema"> <workflows> <workflow name="请假流程" id="1"> <stages> <stage name="主管核准" id="1" ordernum="1"> <users> <user name="James" isAudit="1"/> </users> </stage> </stages> </workflow> </workflows> </WorkflowConfigData>
///示例cs文件 using System; using System.Collections; using System.IO; using System.Text; using System.Web; using System.Xml.Serialization;
namespace Portal.Modules.Workflow { public class WorkflowConfig { public static WorkflowData Settings { get { HttpContext context = HttpContext.Current; WorkflowData data = (WorkflowData) context.Cache["workflowconfig"]; if (data == null) { data = LoadSettings(WorkflowConfig.ConfigFilePath); context.Cache.Insert("workflowconfig", data, new CacheDependency(WorkflowConfig.ConfigFilePath)); } } }
public static String ConfigFilePath { get { HttpContext context = HttpContext.Current; return context.Server.MapPath(context.Request.ApplicationPath + "//" + "Workflow//workflow.xml"); } }
public static WorkflowData LoadSettings(String fileName) { StreamReader reader = File.OpenText(fileName); XmlSerializer serializer = new XmlSerializer(typeof(WorkflowData)); WorkflowData data = (WorkflowData)serializer.Deserialize(reader); reader.Close(); return data; }
public static void PersistSettings(WorkflowData data) { HttpContext context = HttpContext.Current; String configFilePath = context.Server.MapPath(context.Request.ApplicationPath + "//Workflow//workflow.xml"); StreamWriter writer = File.CreateText(configFilePath); XmlSerializer serializer = new XmlSerializer(typeof(WorkflowData)); serializer.Serialize(writer, data); writer.Close(); } }
public class WorkflowData { private Workflow[] _workflows;
[XmlArray] public Workflow [] Workflows { get { return _workflows; } set { _workflows = value; } }
[XmlIgnore] public Workflow this[int workflowID] { get { foreach(Workflow workflow in Workflows) { if(workflowID == workflow.ID) { return workflow; } } return null; } } }
public class Workflow { private String_name; private int_id; private Stage []_stages;
[XmlAttribute] public String Name { get { return _name; } set { _name = value; } }
[XmlAttribute] public int ID { get { return _id; } set { _id = value; } }
[XmlArray] public Stage [] Stages { get { return _stages; } set { _stages = value; } }
[XmlIgnore] public Stage this[int stageid] { get { foreach(Stage stage in Stages) { if(stageid == stage.ID) { return stage; } } return null; } } }
public class Stage { private int_id; private String_name; private int_orderNum; private User []_users;
[XmlAttribute] public String Name { get { return _name; } set { _name = value; } }
[XmlAttribute] public int ID { get { return _id; } set { _id = value; } }
[XmlAttribute] public int OrderNum { get { return _orderNum; } set { _orderNum = value; } }
[XmlArray] public User [] Users { get { return _users; } set { _users = value; } } }
public class User { private bool_isAudit; private String_name;
public bool IsAudit { get { return _isAudit; } set { _isAudit = value; } }
public String Name { get { return _name; } set { _name = value; } } } } 这样我们可以这样来操作数据 workflow[0].stages[0].user[0] //得到用户 类似的我就不说了。(上面的代码不可以直接使用,由于涉及到公司,我删了许多,只是演示而已) 这只是小弟的愚见,还望各位大虾指正 |
关键词: 对象化数据---学习Xml Serialization的一些体会