明輝手游網(wǎng)中心:是一個免費提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺!

對象化數(shù)據(jù)---學(xué)習(xí)Xml Serialization的一些體會

[摘要]剛接觸.net時,就看到過一些用到Xml Serialization的例子,當(dāng)時沒什么感覺。后來看了SDK和一些較大的程序后,逐漸有了點感覺。我個人感覺Xml Serialization 的最大好處...
剛接觸.net時,就看到過一些用到Xml Serialization的例子,當(dāng)時沒什么感覺。后來看了SDK和一些較大的程序后,逐漸有了點感覺。我個人感覺Xml Serialization 的最大好處是可以將一個xml文件對象化,比如說xml中的元素、屬性等都可以對應(yīng)到對象、對象的屬性。這樣我們可以用對象的思想來操作數(shù)據(jù),由于目前主流數(shù)據(jù)庫還只是關(guān)系型的(Oracle也只是部分面向?qū)ο?,我們在數(shù)據(jù)層的操作顯得于其他層面有點隔。而Xml Serialization給了我們這樣一個用面向?qū)ο蟮乃季S來操作數(shù)據(jù)的可能。據(jù)個例子來說,比如說我們要做一個制作工作流程的程序,每個流程有n個階段,每個階段有n個人來完成。如果用數(shù)據(jù)庫做,就不可避免的涉及到多表關(guān)聯(lián)。這對于這樣一個數(shù)據(jù)量比較小的程序來說是一個浪費。我們來看一下怎么利用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="主管核準(zhǔn)" 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;
}
}
}
}
這樣我們可以這樣來操作數(shù)據(jù)
workflow[0].stages[0].user[0] //得到用戶
類似的我就不說了。(上面的代碼不可以直接使用,由于涉及到公司,我刪了許多,只是演示而已)
這只是小弟的愚見,還望各位大蝦指正