利用XML配置实体列表

Xml的好处自然有很多,在软件中用xml可以让软件方便的实现多语言版,在数据传输中它能够以很好的组织结构交换信息,同时利用xpath可以方便的解析,在持久化中它能很好的描述业务数据……

         本文主要讲通过读取xml配置实现对不同数据实体的统一处理,生成表现table,当然生成div也是可以的。以table为例。

代码中没有进行异常处理,请见谅,只是写出了思路!
   
   xml文件:
   

None.gif <? xml version = " 1.0 "  encoding = " utf-8 "   ?>
None.gif
< ListConfig >
None.gif  
<!--  项目table的模板一     type标识模板,attribute定义table或者td的属性,name与数据表的列名对应  -->
None.gif  
< Project type = " 0 "  attribute = " width='100%' " >
None.gif    
< Column name = " Sort "  attribute = " width='35' " > 类别 </ Column >
None.gif    
< Column name = " ProjectName "  attribute = " width='100' " > 项目名称 </ Column >
None.gif    
< Column name = " StudentNum "  attribute = " width='40' " > 学号 </ Column >
None.gif    
< Column name = " Teacher "  attribute = " width='30' " > 老师 </ Column >
None.gif    
< Column name = " TeacherDuty "  attribute = " width='100' " > 老师责任 </ Column >
None.gif  
</ Project >
None.gif  
<!--  项目table的模板二  -->
None.gif  
< Project type = " 1 "  attribute = "" >
None.gif    
< Column name = " Sort "  attribute = " width='35' " > 类别 </ Column >
None.gif    
< Column name = " ProjectName "  attribute = " width='100' " > 项目名称 </ Column >
None.gif    
< Column name = " StudentNum "  attribute = " width='40' " > 学号 </ Column >
None.gif    
< Column name = " Teacher "  attribute = " width='30' " > 老师 </ Column >
None.gif    
< Column name = " TeacherDuty "  attribute = " width='100' " > 老师责任 </ Column >
None.gif    
< Column name = " EduLevel "  attribute = " width='100' " > 教育程度 </ Column >
None.gif  
</ Project >
None.gif  
<!--  项目table的模板三  -->
None.gif  
< Project type = " 2 "  attribute = "" >
None.gif    
< Column name = " ProjectName "  attribute = " width='100' " > 项目名称 </ Column >
None.gif    
< Column name = " StudentNum "  attribute = " width='40' " > 学号 </ Column >
None.gif    
< Column name = " Teacher "  attribute = " width='30' " > 老师 </ Column >
None.gif  
</ Project >
None.gif  
< Article type = " 0 "  attribute = "" >
None.gif    
< Column name = " Title "  attribute = "" > 标题 </ Column >
None.gif    
< Column name = " Content "  attribute = "" > 内容 </ Column >
None.gif    
< Column name = " Author "  attribute = "" > 作者 </ Column >
None.gif    
< Column name = " ComeFrom "  attribute = "" > 来源 </ Column >
None.gif  
</ Article >
None.gif
</ ListConfig >

Code:
None.gif      public   class  ConstructEntityList < T >   where  T :  class new ()
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
private Type tType;
InBlock.gif        
private XmlDocument document = new XmlDocument();
InBlock.gif
InBlock.gif        
public ConstructEntityList()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{   
InBlock.gif            
//获得要生成的列表对应的实体类型
InBlock.gif
            tType = typeof(T);
InBlock.gif            
//加载配置文件
InBlock.gif
            document.Load(@"D:\PracticeAndTeach\PracticeAndTeach\PATWebSite\Component\ListConfig.xml");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获得实体列表table
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="iqueryable">转换源</param>
InBlock.gif        
/// <param name="type">模板类型</param>
ExpandedSubBlockEnd.gif        
/// <returns>table</returns>

InBlock.gif        public string GetEntityListString(IQueryable<T> iqueryable, object type)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//得到模板的列元素
InBlock.gif
            XmlNodeList list = document.SelectNodes(string.Format("/ListConfig/{0}[@type={1}]/Column",tType.Name, Convert.ToInt32(type)));
InBlock.gif            StringBuilder listBuilder 
= new StringBuilder();
InBlock.gif            
//构造table的表头,并且设置table的属性
InBlock.gif
            listBuilder.AppendFormat("<table {0} ><tr>",
InBlock.gif                document.SelectSingleNode(
string.Format("/ListConfig/{0}[@type={1}]",tType.Name, Convert.ToInt32(type))).Attributes.GetNamedItem("attribute").Value);
InBlock.gif            
for (int i = 0; i < list.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                listBuilder.AppendFormat(
"<td {1}>{0}</td>", list.Item(i).InnerText, list.Item(i).Attributes.GetNamedItem("attribute").Value);
ExpandedSubBlockEnd.gif            }

InBlock.gif            listBuilder.Append(
"</tr>");
InBlock.gif
InBlock.gif            PropertyInfo info;
InBlock.gif           
//遍历数据源,通过xml配置文件得到需要读取的实体属性,利用反射得到属性值
InBlock.gif
            foreach (T t in iqueryable)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                listBuilder.Append(
"<tr>");
InBlock.gif                
for (int i = 0; i < list.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    info 
= tType.GetProperty(list.Item(i).Attributes.GetNamedItem("name").Value);
InBlock.gif                    listBuilder.AppendFormat(
"<td>{0}</td>", info.GetValue(t, null));
ExpandedSubBlockEnd.gif                }

InBlock.gif                listBuilder.Append(
"</tr>");
ExpandedSubBlockEnd.gif            }

InBlock.gif            listBuilder.Append(
"</table>");
InBlock.gif            
return listBuilder.ToString();
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

应用:
            调用第一种xml配置
            new ConstructEntityList<Project>().GetEntityListString((from s in patDataContext.Projects select s), ProjectList.Default);
         结果: WebResource.axd?d=pLXXeGbWF7eXU8SMs2-GFZvUWY2JNH05dFx5YzJhGUYAYJAFEaTEq36NAhTPy7_KekvzDFwt8wvQWdByvJIGWdEq6x2KpKD80&t=633043282340000000 untitled.bmp
       调用第二种xml配置
     new ConstructEntityList<Project>().GetEntityListString((from s in patDataContext.Projects select s), ProjectList.More);
         结果:
xml2.bmp

好处:不需要修改源代码,通过对xml配置文件的修改就可以实现table列表值的改变


   场景:在项目管理系统中对于普通用户只需要看到项目名称,项目概述,负责人;对于管理者需要看到项目名称,项目概述,负责人,项目进度,项目成果;哪么我们只需要对不同情况定制一个
xml块,生成列表时只要指定实用那个xml块哪么显示内容自动改变。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值