对Xml的文件操作,根据数据的配置和数据样式的定义,将结果字符输出

using  System;
  
2   using  System.Text;
  
3   using  System.IO;
  
4   using  System.Xml;
  
5   using  TPI;
  
6   using  TPIManager;
  
7  
  
8   namespace  Grid20
  
9  {
 
10       ///   <summary>
  11       ///  对Xml的文件操作,根据数据的配置和数据样式的定义,将结果字符输出
  12       ///   </summary>
  13       public   class  XmlAdapter
 
14      {
 
15           public  XmlAdapter()
 
16          {
 
17          }
 
18           ///   <summary>
  19           ///  配置文件的存放地址
  20           ///   </summary>
  21           private   static   string  m_filepath = System.Web.HttpContext.Current.Server.MapPath( " ./Common/Config.xml " );
 
22          
 
23           // public static string 
  24           ///   <summary>
  25           ///  读取XML的配置文件
  26           ///   </summary>
  27           ///   <param name="filepath"> 文件路径 </param>
  28           ///   <param name="itempath"> Xml路径 </param>
  29           ///   <returns> 节点值数组 </returns>
  30           public   static   string [] ReadItems( string  filepath, string  itempath)
 
31          {
 
32               if ( ! System.IO.File.Exists(filepath))
 
33              {
 
34                   return   new   string [ 1 ];
 
35              }
 
36              System.Xml.XmlDocument doc = new  System.Xml.XmlDocument();
 
37              doc.Load(filepath);
 
38               // 查找根节点
  39              System.Xml.XmlNode root = doc.SelectSingleNode(itempath);
 
40               if (root == null )
 
41              {
 
42                   return   new   string [ 1 ];
 
43              }
 
44  
 
45               string [] ss = new   string [root.ChildNodes.Count]; // (root.ChildNodes.Count);
  46               for ( int  i = 0 ;i < ss.Length;i ++ )
 
47              {
 
48                  ss[i] = root.ChildNodes[i].InnerText;
 
49              }
 
50              root = null ;
 
51               return  ss;
 
52          }
 
53           ///   <summary>
  54           ///  读取XML的配置文件
  55           ///   </summary>
  56           ///   <param name="filepath"> 文件路径 </param>
  57           ///   <param name="itempath"> Xml路径 </param>
  58           ///   <returns> 节点值 </returns>
  59           public   static   string  ReadItem( string  filepath, string  itempath)
 
60          {
 
61               if ( ! System.IO.File.Exists(filepath))
 
62              {
 
63                   return   "" ;
 
64              }
 
65              System.Xml.XmlDocument doc = new  System.Xml.XmlDocument();
 
66              doc.Load(filepath);
 
67  
 
68               // 查找根节点
  69              System.Xml.XmlNode root = doc.SelectSingleNode(itempath);
 
70               if (root == null )
 
71              {
 
72                   return   "" ;
 
73              }
 
74               string  ss = "" ;
 
75               for ( int  i = 0 ;i < root.ChildNodes.Count;i ++ )
 
76              {
 
77                  ss += root.ChildNodes[i].InnerText;
 
78              }
 
79              root = null ;
 
80               return  ss;
 
81          }
 
82           ///   <summary>
  83           ///  读取XML的配置文件
  84           ///   </summary>
  85           ///   <param name="filepath"> 文件路径 </param>
  86           ///   <param name="itempath"> Xml路径 </param>
  87           ///   <returns> 节点值 </returns>
  88           public   static   string  ReadItem( string  filepath, string  itempath, string  item, string  values)
 
89          {
 
90               if ( ! System.IO.File.Exists(filepath))
 
91              {
 
92                   return   "" ;
 
93              }
 
94              System.Xml.XmlDocument doc = new  System.Xml.XmlDocument();
 
95              doc.Load(filepath);
 
96  
 
97               // 查找根节点
  98              System.Xml.XmlNode root = doc.SelectSingleNode(itempath);
 
99               if (root == null )
100              {
101                   return   "" ;
102              }
103               string  ss = "" ;
104               for ( int  i = 0 ;i < root.ChildNodes.Count;i ++ )
105              {
106                  
107                   if (root.ChildNodes[i][item].InnerText == values)
108                  {
109                      ss = root.ChildNodes[i][item].InnerText;
110                       break ;
111                  }
112              }
113              root = null ;
114               return  ss;
115          }
116  
117           ///   <summary>
118           ///   </summary>
119           ///   <param name="filepath"> XML文档路径 </param>
120           ///   <param name="itemname"> 节点名 </param>
121           ///   <returns></returns>
122           private   static  XmlNode GetRoot( string  filepath, string  itemname)
123          {
124               if  ( ! System.IO.File.Exists(filepath))
125              {
126                   return   null ;
127              }
128  
129              System.Xml.XmlDocument doc = new  XmlDocument();
130              doc.Load(filepath);
131               if (doc == null )
132                   return   null ;
133  
134              System.Xml.XmlNodeList root = doc.SelectNodes( " /Root/Items/Item " );
135  
136               if (root == null )
137                   return   null ;
138  
139               foreach   (XmlNode node  in  root)
140              {
141                   if  (node.SelectSingleNode( " Name " ).InnerText == itemname)
142                  {
143                       return  node;
144                  }
145              }
146               return   null ;
147              
148          }
149           ///   <summary>
150           ///   </summary>
151           ///   <param name="node"> 具体节点 </param>
152           ///   <param name="SQL"> 读出的SQL模版 </param>
153           ///   <param name="parameters"> 要替换SQL的字段 </param>
154           ///   <param name="sytle"> 显示风格 </param>
155           ///   <param name="filed"> 需要显示的字段 </param>
156          
157           private   static   void  GetNode(XmlNode node, out   string  sqlstr, out   string [] parameters, out   string  stylehtml, out   string  [] fileds, int  style)
158          {
159              sqlstr = node.SelectSingleNode( " SqlCommand/Main " ).InnerText;
160  
161               // 取得parameter 节点
162              XmlNodeList node_list = node.SelectNodes( " SqlCommand/Parameters/Parameter " );
163              parameters = new   string [node_list.Count];
164               for  ( int  j = 0 ;j < node_list.Count;j ++ )
165              {
166                  parameters[j] = node_list[j].InnerText;
167              }
168              
169               string  path = " ShowItems/ShowItem/Style[@SID= " + style.ToString() + " ] " ;
170  
171              stylehtml = node.SelectSingleNode(path).InnerText;
172  
173               // 取得 filed 节点
174              XmlNodeList filed_list = node.SelectNodes( " ShowItems/ShowItem/DbFields/Field " );
175              fileds = new   string [filed_list.Count];
176               for  ( int  i = 0 ;i < filed_list.Count;i ++ )
177              {
178                  fileds[i] = filed_list[i].InnerText;
179              }
180          }
181           ///   <summary>
182           ///  
183           ///   </summary>
184           ///   <param name="node"> 节点 </param>
185           ///   <returns></returns>
186           private   static   string  GetNodeStyle(XmlNode node)
187          {
188               return  node[ " Style " ].InnerText;
189          }
190       
191           ///   <summary>
192           ///  得到需要显示的内容,直接从配置的数据库表中取得数据
193           ///   </summary>
194           ///   <param name="itemname"> 项名称 </param>
195           ///   <param name="paramters"> 对应的参数列表 </param>
196           ///   <param name="name"> 项目下的子名称 </param>
197           ///   <param name="style"> 样式编号 </param>
198           ///   <param name="ExecuteSql"> 是否执行SQL命令 </param>
199           ///   <returns> 显示的Html代码,参数为空,不使用该参数 </returns>
200           ///   <remarks>
201           ///   </remarks>
202           public   static   string  GetHtml( string  itemname, string [] paramters, string  name, int  style, bool  ExecuteSql)
203          {
204              
205               string  SQL;
206               string [] param;
207               string  style_html;
208               string [] filed;
209  
210               // 取sql命令
211               // 取sql参数
212               // 取显示的样式
213               // 取显示样式的显示项
214              XmlNode root = GetRoot(m_filepath,itemname);
215               if  (root == null )
216              {
217                   return   "" ;
218              }
219               // 得到片头的节点
220               if  (paramters == null )
221              {
222                  style_html = GetNodeStyle(root);
223                   return   string .Format(style_html,itemname);
224              }
225  
226               //
227              GetNode(root, out  SQL, out  param, out  style_html, out  filed,style);
228  
229              
230               if (ExecuteSql)
231              {
232                   // 参数不对应
233              
234                   if  (paramters.Length != param.Length)
235                       return   "" ;
236  
237  
238                   // 组合sql命令
239  
240                   for  ( int  i = 0 ;i < param.Length;i ++ )
241                  {
242                       if  (paramters[i] == "" )
243                           return   "" ;
244                      SQL = SQL.Replace( " { " + param[i].Trim() + " } " ,paramters[i]);
245              
246                  }
247                   // 查询结果
248              
249                  TPI.WebBase m_base = new  WebBase();
250                  m_base.InitComm();
251                  TPI.RecordSet RS = m_base.ConnItem.AddRecordSet(SQL.Replace( " \r\n " , "" ).Trim());
252                  RS.MoveFirst();
253              
254                   // 取显示结果
255  
256                   // 结合样式输出字符串
257                   for  ( int  j = 0 ;j < filed.Length;j ++ )
258                  {  
259                       string  item_value = filed[j].Trim();
260                       string  Item_value;
261                       // 查询的数据库字段
262                       if  (RS.GetValue(item_value) != ""   &&  RS.GetValue(item_value) != null )
263                      {
264                          Item_value = RS.GetValue(item_value);
265                      }
266                       else  
267                      {
268                          Item_value = "" ;
269                      }
270  
271                      style_html = (style_html.Replace( " { " + item_value + " } " ,Item_value));
272                  }
273  
274                  
275              }
276               else // 直接输出结果
277              {
278                   for  ( int  i = 0 ;i < filed.Length;i ++ )
279                  {
280                       if  (paramters[i] == "" )
281                           return   "" ;
282                  
283                      style_html = style_html.Replace( " { " + filed[i].Trim() + " } " ,paramters[i]);
284  
285                  }
286                  
287  
288              }
289               return  style_html.Replace( " {value} " ,name);
290          }
291      }
292  } 

转载于:https://www.cnblogs.com/mousedance/archive/2007/08/03/841962.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值