用dataset操作XML

None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Xml;
None.gif
None.gif
namespace  WebTest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// operateXmlBySataSet 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class operateXmlBySataSet
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public operateXmlBySataSet()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
GetDataSetByXml#region GetDataSetByXml
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 读取xml直接返回DataSet
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="strXmlPath">xml文件相对路径</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public static DataSet GetDataSetByXml(string strXmlPath)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DataSet ds 
= new DataSet();
InBlock.gif                ds.ReadXml(GetXmlFullPath(strXmlPath));
InBlock.gif                
if(ds.Tables.Count > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return ds;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return null;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return null;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
GetDataViewByXml#region GetDataViewByXml
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 读取Xml返回一个经排序或筛选后的DataView
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="strXmlPath"></param>
InBlock.gif        
/// <param name="strWhere">筛选条件,如:"name = 'kgdiwss'"</param>
InBlock.gif        
/// <param name="strSort">排序条件,如:"Id desc"</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public static DataView GetDataViewByXml(string strXmlPath,string strWhere,string strSort)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DataSet ds 
= new DataSet();            
InBlock.gif                ds.ReadXml(GetXmlFullPath(strXmlPath));            
InBlock.gif                DataView dv 
= new DataView(ds.Tables[0]);
InBlock.gif                
if(strSort != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    dv.Sort 
= strSort;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if(strWhere != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    dv.RowFilter 
= strWhere;            
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return dv;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return null;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
WriteXmlByDataSet#region WriteXmlByDataSet
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 向Xml文件插入一行数据
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="strXmlPath">xml文件相对路径</param>
InBlock.gif        
/// <param name="Columns">要插入行的列名数组,如:string[] Columns = {"name","IsMarried"};</param>
InBlock.gif        
/// <param name="ColumnValue">要插入行每列的值数组,如:string[] ColumnValue={"明天去要饭","false"};</param>
ExpandedSubBlockEnd.gif        
/// <returns>成功返回true,否则返回false</returns>

InBlock.gif        public static bool WriteXmlByDataSet(string strXmlPath,string[] Columns,string[] ColumnValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{                    
InBlock.gif                
//根据传入的XML路径得到.XSD的路径,两个文件放在同一个目录下
InBlock.gif
                string strXsdPath = strXmlPath.Substring(0,strXmlPath.IndexOf(".")) + ".xsd";
InBlock.gif                
InBlock.gif                DataSet ds 
= new DataSet();
InBlock.gif                
//读xml架构,关系到列的数据类型
InBlock.gif
                ds.ReadXmlSchema(GetXmlFullPath(strXsdPath));
InBlock.gif                ds.ReadXml(GetXmlFullPath(strXmlPath));
InBlock.gif                DataTable dt 
= ds.Tables[0];
InBlock.gif                
//在原来的表格基础上创建新行
InBlock.gif
                DataRow newRow = dt.NewRow();
InBlock.gif
InBlock.gif                
//循环给一行中的各个列赋值
InBlock.gif
                for(int i=0; i< Columns.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    newRow[Columns[i]] 
= ColumnValue[i];
ExpandedSubBlockEnd.gif                }

InBlock.gif                dt.Rows.Add(newRow);
InBlock.gif                dt.AcceptChanges();
InBlock.gif                ds.AcceptChanges();
InBlock.gif
InBlock.gif                ds.WriteXml(GetXmlFullPath(strXmlPath));    
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion
        
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
UpdateXmlRow#region UpdateXmlRow
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 更行符合条件的一条Xml记录
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="strXmlPath">XML文件路径</param>
InBlock.gif        
/// <param name="Columns">列名数组</param>
InBlock.gif        
/// <param name="ColumnValue">列值数组</param>
InBlock.gif        
/// <param name="strWhereColumnName">条件列名</param>
InBlock.gif        
/// <param name="strWhereColumnValue">条件列值</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public static bool UpdateXmlRow(string strXmlPath,string[] Columns,string[] ColumnValue,string strWhereColumnName,string strWhereColumnValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{                            
InBlock.gif                
string strXsdPath = strXmlPath.Substring(0,strXmlPath.IndexOf(".")) + ".xsd";
InBlock.gif                
InBlock.gif                DataSet ds 
= new DataSet();
InBlock.gif                
//读xml架构,关系到列的数据类型
InBlock.gif
                ds.ReadXmlSchema(GetXmlFullPath(strXsdPath));
InBlock.gif                ds.ReadXml(GetXmlFullPath(strXmlPath));    
InBlock.gif
InBlock.gif                
//先判断行数
InBlock.gif
                if(ds.Tables[0].Rows.Count > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{                    
InBlock.gif                    
for(int i=0; i< ds.Tables[0].Rows.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
//如果当前记录为符合Where条件的记录
InBlock.gif
                        if(ds.Tables[0].Rows[i][strWhereColumnName].ToString().Trim().Equals(strWhereColumnValue))
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
//循环给找到行的各列赋新值
InBlock.gif
                            for(int j=0; j < Columns.Length; j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{                                                            
InBlock.gif                                ds.Tables[
0].Rows[i][Columns[j]] = ColumnValue[j];                                
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
//更新DataSet
InBlock.gif
                            ds.AcceptChanges();
InBlock.gif                            
//重新写入XML文件
InBlock.gif
                             ds.WriteXml(GetXmlFullPath(strXmlPath));
InBlock.gif                            
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
return true;            
InBlock.gif                    
ExpandedSubBlockEnd.gif                }
                    
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion
        
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
DeleteXmlRowByIndex#region DeleteXmlRowByIndex
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 通过删除DataSet中iDeleteRow这一行,然后重写Xml以实现删除指定行
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="strXmlPath"></param>
ExpandedSubBlockEnd.gif        
/// <param name="iDeleteRow">要删除的行在DataSet中的Index值</param>

InBlock.gif        public static bool DeleteXmlRowByIndex(string strXmlPath,int iDeleteRow)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DataSet ds 
= new DataSet();
InBlock.gif                ds.ReadXml(GetXmlFullPath(strXmlPath));
InBlock.gif                
if(ds.Tables[0].Rows.Count > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//删除符号条件的行
InBlock.gif
                    ds.Tables[0].Rows[iDeleteRow].Delete();
ExpandedSubBlockEnd.gif                }

InBlock.gif                ds.WriteXml(GetXmlFullPath(strXmlPath));
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }
            
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
DeleteXmlRows#region DeleteXmlRows
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 删除strColumn列中值为ColumnValue的行
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="strXmlPath">xml相对路径</param>
InBlock.gif        
/// <param name="strColumn">列名</param>
InBlock.gif        
/// <param name="ColumnValue">strColumn列中值为ColumnValue的行均会被删除</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public static bool DeleteXmlRows(string strXmlPath,string strColumn,string[] ColumnValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DataSet ds 
= new DataSet();
InBlock.gif                ds.ReadXml(GetXmlFullPath(strXmlPath));
InBlock.gif
InBlock.gif                
//先判断行数
InBlock.gif
                if(ds.Tables[0].Rows.Count > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//判断行多还是删除的值多,多的for循环放在里面
InBlock.gif
                    if(ColumnValue.Length > ds.Tables[0].Rows.Count)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
for(int i=0; i < ds.Tables[0].Rows.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{                            
InBlock.gif                            
for(int j=0; j < ColumnValue.Length; j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{                        
InBlock.gif                                
if(ds.Tables[0].Rows[i][strColumn].ToString().Trim().Equals(ColumnValue[j]))
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
dot.gif{
InBlock.gif                                    ds.Tables[
0].Rows[i].Delete();
ExpandedSubBlockEnd.gif                                }

ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
for(int j=0; j < ColumnValue.Length; j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{                            
InBlock.gif                            
for(int i=0; i < ds.Tables[0].Rows.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{                                    
InBlock.gif                                
if(ds.Tables[0].Rows[i][strColumn].ToString().Trim().Equals(ColumnValue[j]))
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
dot.gif{
InBlock.gif                                    ds.Tables[
0].Rows[i].Delete();
ExpandedSubBlockEnd.gif                                }

ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockEnd.gif                        }
                        
ExpandedSubBlockEnd.gif                    }
        
InBlock.gif                    ds.WriteXml(GetXmlFullPath(strXmlPath));
ExpandedSubBlockEnd.gif                }
                
InBlock.gif                
return true;        
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }
            
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif        
ContractedSubBlock.gifExpandedSubBlockStart.gif        
DeleteXmlAllRows#region DeleteXmlAllRows
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 删除所有行
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="strXmlPath">XML路径</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public static bool DeleteXmlAllRows(string strXmlPath)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DataSet ds 
= new DataSet();
InBlock.gif                ds.ReadXml(GetXmlFullPath(strXmlPath));
InBlock.gif                
//如果记录条数大于0
InBlock.gif
                if(ds.Tables[0].Rows.Count > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//移除所有记录
InBlock.gif
                    ds.Tables[0].Rows.Clear();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
//重新写入,这时XML文件中就只剩根节点了
InBlock.gif
                ds.WriteXml(GetXmlFullPath(strXmlPath));                
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }
        
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
GetXmlFullPath#region GetXmlFullPath
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 返回完整路径
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="strPath">Xml的路径</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public static string GetXmlFullPath(string strPath)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{        
InBlock.gif            
if(strPath.IndexOf(":"> 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return strPath;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return System.Web.HttpContext.Current.Server.MapPath(strPath);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/DODONG/archive/2005/09/27/245510.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值