Xml的好处自然有很多,在软件中用xml可以让软件方便的实现多语言版,在数据传输中它能够以很好的组织结构交换信息,同时利用xpath可以方便的解析,在持久化中它能很好的描述业务数据……
本文主要讲通过读取xml配置实现对不同数据实体的统一处理,生成表现table,当然生成div也是可以的。以table为例。
代码中没有进行异常处理,请见谅,只是写出了思路!
xml文件:
<?
xml version
=
"
1.0
"
encoding
=
"
utf-8
"
?>
< ListConfig >
<!-- 项目table的模板一 type标识模板,attribute定义table或者td的属性,name与数据表的列名对应 -->
< Project type = " 0 " attribute = " width='100%' " >
< Column name = " Sort " attribute = " width='35' " > 类别 </ Column >
< Column name = " ProjectName " attribute = " width='100' " > 项目名称 </ Column >
< Column name = " StudentNum " attribute = " width='40' " > 学号 </ Column >
< Column name = " Teacher " attribute = " width='30' " > 老师 </ Column >
< Column name = " TeacherDuty " attribute = " width='100' " > 老师责任 </ Column >
</ Project >
<!-- 项目table的模板二 -->
< Project type = " 1 " attribute = "" >
< Column name = " Sort " attribute = " width='35' " > 类别 </ Column >
< Column name = " ProjectName " attribute = " width='100' " > 项目名称 </ Column >
< Column name = " StudentNum " attribute = " width='40' " > 学号 </ Column >
< Column name = " Teacher " attribute = " width='30' " > 老师 </ Column >
< Column name = " TeacherDuty " attribute = " width='100' " > 老师责任 </ Column >
< Column name = " EduLevel " attribute = " width='100' " > 教育程度 </ Column >
</ Project >
<!-- 项目table的模板三 -->
< Project type = " 2 " attribute = "" >
< Column name = " ProjectName " attribute = " width='100' " > 项目名称 </ Column >
< Column name = " StudentNum " attribute = " width='40' " > 学号 </ Column >
< Column name = " Teacher " attribute = " width='30' " > 老师 </ Column >
</ Project >
< Article type = " 0 " attribute = "" >
< Column name = " Title " attribute = "" > 标题 </ Column >
< Column name = " Content " attribute = "" > 内容 </ Column >
< Column name = " Author " attribute = "" > 作者 </ Column >
< Column name = " ComeFrom " attribute = "" > 来源 </ Column >
</ Article >
</ ListConfig >
< ListConfig >
<!-- 项目table的模板一 type标识模板,attribute定义table或者td的属性,name与数据表的列名对应 -->
< Project type = " 0 " attribute = " width='100%' " >
< Column name = " Sort " attribute = " width='35' " > 类别 </ Column >
< Column name = " ProjectName " attribute = " width='100' " > 项目名称 </ Column >
< Column name = " StudentNum " attribute = " width='40' " > 学号 </ Column >
< Column name = " Teacher " attribute = " width='30' " > 老师 </ Column >
< Column name = " TeacherDuty " attribute = " width='100' " > 老师责任 </ Column >
</ Project >
<!-- 项目table的模板二 -->
< Project type = " 1 " attribute = "" >
< Column name = " Sort " attribute = " width='35' " > 类别 </ Column >
< Column name = " ProjectName " attribute = " width='100' " > 项目名称 </ Column >
< Column name = " StudentNum " attribute = " width='40' " > 学号 </ Column >
< Column name = " Teacher " attribute = " width='30' " > 老师 </ Column >
< Column name = " TeacherDuty " attribute = " width='100' " > 老师责任 </ Column >
< Column name = " EduLevel " attribute = " width='100' " > 教育程度 </ Column >
</ Project >
<!-- 项目table的模板三 -->
< Project type = " 2 " attribute = "" >
< Column name = " ProjectName " attribute = " width='100' " > 项目名称 </ Column >
< Column name = " StudentNum " attribute = " width='40' " > 学号 </ Column >
< Column name = " Teacher " attribute = " width='30' " > 老师 </ Column >
</ Project >
< Article type = " 0 " attribute = "" >
< Column name = " Title " attribute = "" > 标题 </ Column >
< Column name = " Content " attribute = "" > 内容 </ Column >
< Column name = " Author " attribute = "" > 作者 </ Column >
< Column name = " ComeFrom " attribute = "" > 来源 </ Column >
</ Article >
</ ListConfig >
Code:
public
class
ConstructEntityList
<
T
>
where
T :
class
,
new
()
{
private Type tType;
private XmlDocument document = new XmlDocument();
public ConstructEntityList()
{
//获得要生成的列表对应的实体类型
tType = typeof(T);
//加载配置文件
document.Load(@"D:\PracticeAndTeach\PracticeAndTeach\PATWebSite\Component\ListConfig.xml");
}
/**//// <summary>
/// 获得实体列表table
/// </summary>
/// <param name="iqueryable">转换源</param>
/// <param name="type">模板类型</param>
/// <returns>table</returns>
public string GetEntityListString(IQueryable<T> iqueryable, object type)
{
//得到模板的列元素
XmlNodeList list = document.SelectNodes(string.Format("/ListConfig/{0}[@type={1}]/Column",tType.Name, Convert.ToInt32(type)));
StringBuilder listBuilder = new StringBuilder();
//构造table的表头,并且设置table的属性
listBuilder.AppendFormat("<table {0} ><tr>",
document.SelectSingleNode(string.Format("/ListConfig/{0}[@type={1}]",tType.Name, Convert.ToInt32(type))).Attributes.GetNamedItem("attribute").Value);
for (int i = 0; i < list.Count; i++)
{
listBuilder.AppendFormat("<td {1}>{0}</td>", list.Item(i).InnerText, list.Item(i).Attributes.GetNamedItem("attribute").Value);
}
listBuilder.Append("</tr>");
PropertyInfo info;
//遍历数据源,通过xml配置文件得到需要读取的实体属性,利用反射得到属性值
foreach (T t in iqueryable)
{
listBuilder.Append("<tr>");
for (int i = 0; i < list.Count; i++)
{
info = tType.GetProperty(list.Item(i).Attributes.GetNamedItem("name").Value);
listBuilder.AppendFormat("<td>{0}</td>", info.GetValue(t, null));
}
listBuilder.Append("</tr>");
}
listBuilder.Append("</table>");
return listBuilder.ToString();
}
}
{
private Type tType;
private XmlDocument document = new XmlDocument();
public ConstructEntityList()
{
//获得要生成的列表对应的实体类型
tType = typeof(T);
//加载配置文件
document.Load(@"D:\PracticeAndTeach\PracticeAndTeach\PATWebSite\Component\ListConfig.xml");
}
/**//// <summary>
/// 获得实体列表table
/// </summary>
/// <param name="iqueryable">转换源</param>
/// <param name="type">模板类型</param>
/// <returns>table</returns>
public string GetEntityListString(IQueryable<T> iqueryable, object type)
{
//得到模板的列元素
XmlNodeList list = document.SelectNodes(string.Format("/ListConfig/{0}[@type={1}]/Column",tType.Name, Convert.ToInt32(type)));
StringBuilder listBuilder = new StringBuilder();
//构造table的表头,并且设置table的属性
listBuilder.AppendFormat("<table {0} ><tr>",
document.SelectSingleNode(string.Format("/ListConfig/{0}[@type={1}]",tType.Name, Convert.ToInt32(type))).Attributes.GetNamedItem("attribute").Value);
for (int i = 0; i < list.Count; i++)
{
listBuilder.AppendFormat("<td {1}>{0}</td>", list.Item(i).InnerText, list.Item(i).Attributes.GetNamedItem("attribute").Value);
}
listBuilder.Append("</tr>");
PropertyInfo info;
//遍历数据源,通过xml配置文件得到需要读取的实体属性,利用反射得到属性值
foreach (T t in iqueryable)
{
listBuilder.Append("<tr>");
for (int i = 0; i < list.Count; i++)
{
info = tType.GetProperty(list.Item(i).Attributes.GetNamedItem("name").Value);
listBuilder.AppendFormat("<td>{0}</td>", info.GetValue(t, null));
}
listBuilder.Append("</tr>");
}
listBuilder.Append("</table>");
return listBuilder.ToString();
}
}
应用:
调用第一种xml配置
new ConstructEntityList<Project>().GetEntityListString((from s in patDataContext.Projects select s), ProjectList.Default);
结果:
调用第二种xml配置
new ConstructEntityList<Project>().GetEntityListString((from s in patDataContext.Projects select s), ProjectList.More);
结果:
好处:不需要修改源代码,通过对xml配置文件的修改就可以实现table列表值的改变
场景:在项目管理系统中对于普通用户只需要看到项目名称,项目概述,负责人;对于管理者需要看到项目名称,项目概述,负责人,项目进度,项目成果;哪么我们只需要对不同情况定制一个xml块,生成列表时只要指定实用那个xml块哪么显示内容自动改变。