XML通用操作类

  1  using  System;
  2  using  System.Xml;
  3  using  System.Data;
  4 
  5  namespace  Message.Common
  6  {
  7           ///   <summary>
  8           ///  XML相关通用功能
  9           ///   </summary>
 10           public   class  Xml
 11          {
 12                   public  Xml() {}        
 13 
 14           ///   <summary>
 15           ///  XML资源类型
 16           ///   </summary>
 17           public   enum  XmlType
 18          {
 19              File,
 20              String
 21          };
 22 
 23                   #region  读取XML资源到DataSet中
 24           ///   <summary>
 25           ///  读取XML资源到DataSet中
 26           ///   </summary>
 27           ///   <param name="source"> XML资源,文件为路径,否则为XML字符串 </param>
 28           ///   <param name="xmlType"> XML资源类型 </param>
 29           ///   <returns> DataSet </returns>
 30           public   static  DataSet GetDataSet( string  source, XmlType xmlType)
 31          {
 32              DataSet ds  =   new  DataSet();
 33               if  (xmlType  ==  XmlType.File)
 34              {
 35                  ds.ReadXml(source);
 36              }
 37               else
 38              {
 39                  XmlDocument xd  =   new  XmlDocument();
 40                  xd.LoadXml(source);
 41                  XmlNodeReader xnr  =   new  XmlNodeReader(xd);
 42                  ds.ReadXml(xnr);
 43              }
 44 
 45               return  ds;
 46          }
 47 
 48                   #endregion
 49 
 50                   #region  获取一个字符串xml文档中的ds
 51                   /// <remarks>
 52                   /// 赵洪
 53                   /// </remarks>
 54                   ///   <summary>
 55                   ///  获取一个字符串xml文档中的ds
 56                   ///   </summary>
 57                   ///   <param name="xml_string"> 含有xml信息的字符串 </param>
 58                   public   static   void  get_XmlValue_ds( string  xml_string, ref  DataSet ds)
 59                  {
 60                         
 61                          System.Xml.XmlDocument xd =   new  XmlDocument();
 62                          xd.LoadXml(xml_string);
 63                          XmlNodeReader xnr =   new  XmlNodeReader(xd);
 64                          ds.ReadXml(xnr);
 65                          xnr.Close();
 66                           int  a =  ds.Tables.Count;                       
 67 
 68                  }
 69 
 70                   #endregion
 71 
 72                   #region   读取XML资源到DataTable中
 73           ///   <summary>
 74           ///  读取XML资源到DataTable中
 75           ///   </summary>
 76           ///   <param name="source"> XML资源,文件为路径,否则为XML字符串 </param>
 77           ///   <param name="xmlType"> XML资源类型:文件,字符串 </param>
 78           ///   <param name="tableName"> 表名称 </param>
 79           ///   <returns> DataTable </returns>
 80           public   static  DataTable GetTable( string  source, XmlType xmlType,  string  tableName)
 81          {
 82              DataSet ds  =   new  DataSet();
 83               if  (xmlType  ==  XmlType.File)
 84              {
 85                  ds.ReadXml(source);
 86              }
 87               else
 88              {
 89                  XmlDocument xd  =   new  XmlDocument();
 90                  xd.LoadXml(source);
 91                  XmlNodeReader xnr  =   new  XmlNodeReader(xd);
 92                  ds.ReadXml(xnr);
 93              }
 94 
 95               return  ds.Tables[tableName];
 96          }
 97 
 98                   #endregion
 99 
100                   #region  读取XML资源中指定的DataTable的指定行指定列的值
101           ///   <summary>
102           ///  读取XML资源中指定的DataTable的指定行指定列的值
103           ///   </summary>
104           ///   <param name="source"> XML资源 </param>
105           ///   <param name="xmlType"> XML资源类型:文件,字符串 </param>
106           ///   <param name="tableName"> 表名 </param>
107           ///   <param name="rowIndex"> 行号 </param>
108           ///   <param name="colName"> 列名 </param>
109           ///   <returns> 值,不存在时返回Null </returns>
110           public   static   object  GetTableCell( string  source, XmlType xmlType,  string  tableName,  int  rowIndex,  string  colName)
111          {
112              DataSet ds  =   new  DataSet();
113               if  (xmlType  ==  XmlType.File)
114              {
115                  ds.ReadXml(source);
116              }
117               else
118              {
119                  XmlDocument xd  =   new  XmlDocument();
120                  xd.LoadXml(source);
121                  XmlNodeReader xnr  =   new  XmlNodeReader(xd);
122                  ds.ReadXml(xnr);
123              }
124 
125               return  ds.Tables[tableName].Rows[rowIndex][colName];
126          }
127 
128                   #endregion
129 
130                   #region  读取XML资源中指定的DataTable的指定行指定列的值
131           ///   <summary>
132           ///  读取XML资源中指定的DataTable的指定行指定列的值
133           ///   </summary>
134           ///   <param name="source"> XML资源 </param>
135           ///   <param name="xmlType"> XML资源类型:文件,字符串 </param>
136           ///   <param name="tableName"> 表名 </param>
137           ///   <param name="rowIndex"> 行号 </param>
138           ///   <param name="colIndex"> 列号 </param>
139           ///   <returns> 值,不存在时返回Null </returns>
140           public   static   object  GetTableCell( string  source, XmlType xmlType,  string  tableName,  int  rowIndex,  int  colIndex)
141          {
142              DataSet ds  =   new  DataSet();
143               if  (xmlType  ==  XmlType.File)
144              {
145                  ds.ReadXml(source);
146              }
147               else
148              {
149                  XmlDocument xd  =   new  XmlDocument();
150                  xd.LoadXml(source);
151                  XmlNodeReader xnr  =   new  XmlNodeReader(xd);
152                  ds.ReadXml(xnr);
153              }
154 
155               return  ds.Tables[tableName].Rows[rowIndex][colIndex];
156          }        
157 
158                   #endregion
159 
160                   #region   获取一个字符串xml文档中的一个table,指定行,指定列的值
161                   ///   <summary>
162                   ///  获取一个字符串xml文档中的一个table,指定行,指定列的值
163                   ///   </summary>
164                   ///   <param name="xml_string"> 含有xml信息的字符串 </param>
165                   ///   <param name="tablename"> 表名 </param>
166                   ///   <param name="row_index"> 指定行 </param>
167                   ///   <param name="col_name"> 指定列名 </param>
168                   ///   <returns> 相应节点的值 </returns>
169                   public   static   string  get_XmlValue( string  xml_string, string  tablename, int  row_index, string  col_name)
170                  {
171                          System.Xml.XmlDocument xd =   new  XmlDocument();
172                          xd.LoadXml(xml_string);
173                          XmlElement root  =  xd.DocumentElement;
174                          XmlNode xe =  root.CloneNode( false );
175                          xe  =  root.SelectNodes(tablename).Item(row_index);
176                           string  val = "" ;
177                           if ( null == xe)
178                          {
179                                   return   "" ;
180                          }
181                           foreach (XmlNode xn  in  xe.ChildNodes )
182                          {
183                                   if (xn.LocalName == col_name)
184                                  {
185                                          val  =  xn.InnerText;
186                                           break ;
187                                  }
188                          }
189                           return  val;
190                  }
191 
192                   ///   <summary>
193                   ///  获取一个xml文件中的一个table,指定行,指定列的值
194                   ///   </summary>
195                   ///   <param name="xml_string"> 含有xml信息的字符串 </param>
196                   ///   <param name="tablename"> 表名 </param>
197                   ///   <param name="row_index"> 指定行 </param>
198                   ///   <param name="col_name"> 指定列名 </param>
199                   ///   <returns> 相应节点的值 </returns>
200                   public   static   string  get_XmlValue( string  xml_string, string  tablename, int  row_index, string  col_name, bool  isfile)
201                  {
202                          System.Xml.XmlDocument xd =   new  XmlDocument();
203                           if (isfile)
204                                  xd.Load(xml_string);
205                           else
206                                  xd.LoadXml(xml_string);
207                          XmlElement root  =  xd.DocumentElement;
208                          XmlNode xe =  root.CloneNode( false );
209                          xe  =  root.SelectNodes(tablename).Item(row_index);
210                           string  val = "" ;
211                           if ( null == xe)
212                          {
213                                   return   "" ;
214                          }
215                           foreach (XmlNode xn  in  xe.ChildNodes )
216                          {
217                                   if (xn.LocalName == col_name)
218                                  {
219                                          val  =  xn.InnerText;
220                                           break ;
221                                  }
222                          }
223                           return  val;
224                  }
225 
226                   #endregion
227 
228                   #region  获取一个字符串xml文档中的dt
229                   /// <remarks>
230                   ///
231                   /// </remarks>
232                   ///   <summary>
233                   ///  获取一个字符串xml文档中的dt
234                   ///   </summary>
235                   ///   <param name="xml_string"> 含有xml信息的字符串 </param>
236                   public   static   void  get_XmlValue_dt( string  xml_string, ref  DataTable dt, string  table_name)
237                  {
238                          DataSet ds  =   new  DataSet();
239                          System.Xml.XmlDocument xd =   new  XmlDocument();
240                          xd.LoadXml(xml_string);
241                           // System.IO.FileStream fs = new System.IO.FileStream("c:\\aa.xml",System.IO.FileMode.Open);
242                         
243                           // xd.Save(fs);
244                           //                         System.Xml.XmlReader xr=(System.Xml.XmlReader)((object)sr);
245                           // ds=new DataSet();
246                          XmlNodeReader xnr =   new  XmlNodeReader(xd);
247                          ds.ReadXml(xnr);
248                          xnr.Close();
249                          dt  =  ds.Tables[table_name];
250                  }
251                   #endregion
252 
253                   #region   将DataTable写入XML文件中
254           ///   <summary>
255           ///  将DataTable写入XML文件中
256           ///   </summary>
257           ///   <param name="dt"> 含有数据的DataTable </param>
258           ///   <param name="filePath"> 文件路径 </param>
259           public   static   void  SaveTableToFile(DataTable dt,  string  filePath)
260          {
261              DataSet ds  =   new  DataSet( " Config " );
262              ds.Tables.Add(dt.Copy());
263 
264              ds.WriteXml(filePath);
265          }
266                   #endregion
267 
268                   #region   将DataTable以指定的根结点名称写入文件
269           ///   <summary>
270           ///  将DataTable以指定的根结点名称写入文件
271           ///   </summary>
272           ///   <param name="dt"> 含有数据的DataTable </param>
273           ///   <param name="rootName"> 根结点名称 </param>
274           ///   <param name="filePath"> 文件路径 </param>
275           public   static   void  SaveTableToFile(DataTable dt,  string  rootName,  string  filePath)
276          {
277              DataSet ds  =   new  DataSet(rootName);
278              ds.Tables.Add(dt.Copy());
279 
280              ds.WriteXml(filePath);
281          }
282                   #endregion
283 
284                   #region  使用DataSet方式更新XML文件节点
285 
286           ///   <summary>
287           ///  使用DataSet方式更新XML文件节点
288           ///   </summary>
289           ///   <param name="filePath"> XML文件路径 </param>
290           ///   <param name="tableName"> 表名称 </param>
291           ///   <param name="rowIndex"> 行号 </param>
292           ///   <param name="colName"> 列名 </param>
293           ///   <param name="content"> 更新值 </param>
294           ///   <returns> 更新是否成功 </returns>
295           public   static   bool  UpdateTableCell( string  filePath,  string  tableName,  int  rowIndex,  string  colName,  string  content)
296          {
297               bool  flag  =   false ;
298 
299              DataSet ds  =   new  DataSet();
300              ds.ReadXml(filePath);
301              DataTable dt  =  ds.Tables[tableName];
302              
303               if  (dt.Rows[rowIndex][colName]  !=   null )
304              {
305                  dt.Rows[rowIndex][colName]  =  content;
306                  ds.WriteXml(filePath);
307                  flag  =   true ;
308              }
309               else
310              {
311                  flag  =   false ;
312              }
313 
314               return  flag;
315          }
316 
317                   #endregion
318 
319                   #region   使用DataSet方式更新XML文件节点
320           ///   <summary>
321           ///  使用DataSet方式更新XML文件节点
322           ///   </summary>
323           ///   <param name="filePath"> XML文件路径 </param>
324           ///   <param name="tableName"> 表名称 </param>
325           ///   <param name="rowIndex"> 行号 </param>
326           ///   <param name="colIndex"> 列号 </param>
327           ///   <param name="content"> 更新值 </param>
328           ///   <returns> 更新是否成功 </returns>
329           public   static   bool  UpdateTableCell( string  filePath,  string  tableName,  int  rowIndex,  int  colIndex,  string  content)
330          {
331               bool  flag  =   false ;
332 
333              DataSet ds  =   new  DataSet();
334              ds.ReadXml(filePath);
335              DataTable dt  =  ds.Tables[tableName];
336              
337               if  (dt.Rows[rowIndex][colIndex]  !=   null )
338              {
339                  dt.Rows[rowIndex][colIndex]  =  content;
340                  ds.WriteXml(filePath);
341                  flag  =   true ;
342              }
343               else
344              {
345                  flag  =   false ;
346              }
347 
348               return  flag;
349          }
350           #endregion
351 
352                   #region  读取XML资源中的指定节点内容
353 
354           ///   <summary>
355           ///  读取XML资源中的指定节点内容
356           ///   </summary>
357           ///   <param name="source"> XML资源 </param>
358           ///   <param name="xmlType"> XML资源类型:文件,字符串 </param>
359           ///   <param name="nodeName"> 节点名称 </param>
360           ///   <returns> 节点内容 </returns>
361           public   static   object  GetNodeValue( string  source, XmlType xmlType,  string  nodeName)
362          {
363              XmlDocument xd  =   new  XmlDocument();
364               if  (xmlType  ==  XmlType.File)
365              {
366                  xd.Load(source);
367              }
368               else
369              {
370                  xd.LoadXml(source);
371              }
372              XmlElement xe  =  xd.DocumentElement;
373              XmlNode xn  =  xe.SelectSingleNode( " // "   +  nodeName);
374               if (xn  !=   null )
375              {
376                   return  xn.InnerText;
377              }
378               else
379              {
380                   return   null ;
381              }
382          }
383                   ///   <summary>
384                   ///  读取XML资源中的指定节点内容
385                   ///   </summary>
386                   ///   <param name="source"> XML资源 </param>
387                   ///   <param name="nodeName"> 节点名称 </param>
388                   ///   <returns> 节点内容 </returns>
389                   public   static   object  GetNodeValue( string  source, string  nodeName)
390                  {
391                           if (source  ==   null   ||  nodeName  ==   null   ||  source  ==   ""   ||  nodeName  ==   ""   ||  source.Length  <  nodeName.Length  *   2 )
392                          {
393                                   return   null ;
394                          }
395                           else
396                          {
397                                   int  start  =  source.IndexOf( " < "   +  nodeName  +   " > " +  nodeName.Length  +   2 ;
398                                   int  end  =  source.IndexOf( " </ "   +  nodeName  +   " > " );
399                                   if (start  ==   - 1   ||  end  ==   - 1 )
400                                  {
401                                           return   null ;
402                                  }
403                                   else   if (start  >=  end)
404                                  {
405                                           return   null ;
406                                  }
407                                   else
408                                  {
409                                           return  source.Substring(start,end  -  start);
410                                  }
411                          }
412                  }
413 
414 
415                   #endregion
416 
417                   #region  更新XML文件中的指定节点内容
418           ///   <summary>
419           ///  更新XML文件中的指定节点内容
420           ///   </summary>
421           ///   <param name="filePath"> 文件路径 </param>
422           ///   <param name="nodeName"> 节点名称 </param>
423           ///   <param name="nodeValue"> 更新内容 </param>
424           ///   <returns> 更新是否成功 </returns>
425           public   static   bool  UpdateNode( string  filePath,  string  nodeName,  string  nodeValue)
426          {
427               bool  flag  =   false ;
428 
429              XmlDocument xd  =   new  XmlDocument();
430              xd.Load(filePath);
431              XmlElement xe  =  xd.DocumentElement;
432              XmlNode xn  =  xe.SelectSingleNode( " // "   +  nodeName);
433               if  (xn  !=   null )
434              {
435                  xn.InnerText  =  nodeValue;
436                  flag  =   true ;
437              }
438               else
439              {
440                  flag  =   false ;
441              }
442 
443               return  flag;
444          }
445                   #endregion
446 
447                   #region  操作xml文件中指定节点的数据
448                   ///   <summary>
449                   ///  获得xml文件中指定节点的节点数据
450                   ///   </summary>
451                   ///   <param name="TableName"></param>
452                   ///   <returns></returns>
453                   public   static   string  GetNodeInfoByNodeName( string  path, string  nodeName)
454                  {                       
455                           string  XmlString = "" ;
456                          XmlDocument xml = new  XmlDocument();
457                          xml.Load(path);
458                          System.Xml.XmlElement root = xml.DocumentElement;
459                          System.Xml.XmlNode node = root.SelectSingleNode( " // " + nodeName);
460                           if  (node != null )
461                          {
462                                  XmlString = node.InnerText;
463                          }
464                           return  XmlString;
465                  }
466                   #endregion
467 
468                   ///   <summary>
469                   ///  根据xml路径获取DataSet。如果Table名为空:flag=false 返回所有xml的数据;flag=true 将xml中的table名作为数据项返回。否则根据table名获取相应的table信息返回。
470                   ///   </summary>
471                   ///   <param name="XmlPath"> xml文件路径 </param>
472                   ///   <param name="TableName"> 所要获取的Table名,可为空 </param>
473                   ///   <param name="flag"> 若为true,则只将所有表名作为数据项返回;若为false,则返回所要获取的Table的所有数据 </param>
474                   ///   <returns> 返回所获取的DataSet </returns>
475                   ///   <summary>
476                   ///  根据xml路径获取DataSet。如果Table名为空:flag=false 返回所有xml的数据;flag=true 将xml中的table名作为数据项返回。否则根据table名获取相应的table信息返回。
477                   ///   </summary>
478                   ///   <param name="XmlPath"> xml文件路径 </param>
479                   ///   <param name="TableName"> 所要获取的Table名,可为空 </param>
480                   ///   <param name="flag"> 若为true,则只将所有表名作为数据项返回;若为false,则返回所要获取的Table的所有数据 </param>
481                   ///   <returns> 返回所获取的DataSet </returns>
482                   public   static  DataSet GetTableByXml( string  XmlPath, string  TableName, bool  flag)
483                  {
484                          DataSet ds = new  DataSet();
485                           if  (TableName == "" )
486                          {
487                                  DataSet ds1 = new  DataSet();
488                                  ds1.ReadXml(XmlPath);
489                                   if  (ds1.Tables.Count > 0 )
490                                  {
491                                           if  (flag)
492                                          {
493                                                  DataTable dt = new  DataTable( " typeTable " );
494                                                  dt.Columns.Add( " TableName " , typeof ( string ));
495                                                  ds.Tables.Add(dt);
496                                                   for  ( int  i = 0 ;i < ds1.Tables.Count;i ++ )
497                                                  {
498                                                          DataRow dr = dt.NewRow();
499                                                          dr[ " TableName " ] = ds1.Tables[i].TableName;
500                                                          ds.Tables[ " typeTable " ].Rows.Add(dr);
501                                                  }
502                                          }
503                                           else
504                                          {
505                                                  ds = ds1.Copy();
506                                          }
507                                  }
508                          }
509                           else
510                          {
511                                  DataSet ds2 = new  DataSet();
512                                  ds2.ReadXml(XmlPath);
513                                   if  (ds2.Tables[TableName] != null )
514                                  {
515                                          ds.Tables.Add(ds2.Tables[TableName].Copy());
516                                  }
517                          }
518                           return  ds;                       
519                  }
520                   ///   <summary>
521                   ///  escape invalid Unicode in XML
522                   ///   </summary>
523                   ///   <param name="str"></param>
524                   ///   <returns></returns>
525                   public   static   string  Replaceinvalid( string  str)
526                  {
527                          System.Text.RegularExpressions.Regex r  =   new  System.Text.RegularExpressions.Regex( " [\x00-\x08|\x0b-\x0c|\x0e-\x1f] " );
528                           return  r.Replace(str, "   " );
529                  }
530 
531                   ///   <summary>
532                   ///  获得接口错误信息
533                   ///   </summary>
534                   ///   <param name="errCode"> 错误编码 </param>
535                   ///   <returns></returns>
536                   public   static   string  GetInterfaceErrorString( string  errCode)
537                  {
538                          System.Text.StringBuilder sb  =   new  System.Text.StringBuilder();
539                          sb.Append( " <?xml version=\ " 1.0 \ "  encoding=\ " GB2312\ " ?> " );
540                          sb.Append( " <Root> " );
541                          sb.Append( " <Result><return_result> " + errCode + " </return_result></Result> " );
542                          sb.Append( " </Root> " );
543                         
544                           return  sb.ToString();
545                  }
546          }
547 

 

转载于:https://www.cnblogs.com/taony/archive/2010/10/25/1860205.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值