你的分享就是我们的动力 ---﹥

XML操作集合

时间:2011-10-08 21:43来源:www.chengxuyuans.com 点击:

 

DataSet操作XML,归根到底就是对DataSet里的表格,行,列等进行操作,然后用DataSet里的东西重新写到XML中,从而实现编辑XML的目的。如果再配合上.xsd文件的话,那效果更佳。

一、  读操作

11读取XML的数据到DataSet中的方法(全部读取)为

(调用部分)

DataGrid1.DataSource = OperateXmlByDataSet.GetDataSetByXml(@"xml/xml_xmlDB.xml");
DataGrid1.DataBind();

(方法部分)

#region GetDataSetByXml
/// <summary>
/// 
读取xml直接返回DataSet
/// </summary>
/// <param name="strXmlPath">xml
文件相对路径</param>
 
/// <returns></returns>
public static DataSet GetDataSetByXml(string strXmlPath)
{
try
{
DataSet ds = new DataSet();
//
读取XMLDataSet
ds.ReadXml(GetXmlFullPath(strXmlPath));// GetXmlFullPath()//
此方法用于将相对路径转为全局路径
if(ds.Tables.Count > 0)
{
return ds;
}
return null;
}
catch(Exception)
{
return null;
}
}
#endregion

12读取XML的数据到DataSet中(条件选择)的方法为

(调用部分)

DataGrid1.DataSource = OperateXmlByDataSet.GetDataViewByXml(
@"xml/xml_xmlDB.xml", //XML
文件路径
"name = 'Asp.net'", //
条件:name列值为Asp.net
"peopleNum desc"); //
peopleNum列降序排列
DataGrid1.DataBind();

(方法部分)

#region GetDataViewByXml

                   /// summary

                   /// 读取Xml返回一个经排序或筛选后的DataView

                   /// /summary

                   /// param name="strXmlPath"〉〈/param

                   /// param name="strWhere"〉筛选条件,如:"name = 'kgdiwss'"/param

                   /// param name="strSort"〉排序条件,如:"Id desc"/param

                   /// returns〉〈/returns

                   public static DataView GetDataViewByXml(string strXmlPath,string strWhere,string strSort)

                   {

                            try

                            {

                                     DataSet ds = new DataSet();                   

                                     ds.ReadXml(GetXmlFullPath(strXmlPath));

                   //创建DataView来完成排序或筛选操作               

                                     DataView dv = new DataView(ds.Tables[0]);

                                     if(strSort != null)

                                     {

                        //DataView中的记录进行排序

                                               dv.Sort = strSort;

                                     }

                                     if(strWhere != null)

                                     {

                                               //DataView中的记录进行筛选,找到我们想要的记录

                                               dv.RowFilter = strWhere;                       

                                     }

                                     return dv;

                            }

                            catch(Exception)

                            {

                                     return null;

                            }

                   }

                   #endregion

 

一、      增加操作

(调用部分)

bool b;
b = OperateXmlByDataSet.WriteXmlByDataSet(
@"xml/xml_xmlDB.xml", //XML
文件地址
new string[]{
"name", //
姓名字段
"peopleNum", //
人数字段
"address", //
地址字段
"description", //
描述字段
"require", //
需求字段
"deadLine", //
结束时间字段
"IsMarried" //
婚否字段
},
new string[]{
"Asp.net
程序员", //姓名字段值
"2", //
人数字段值
"
建国路", //地址字段值
"B/S
结构程序", //描述字段值
"asp.net c#
", //需求字段值
DateTime.Now.ToShortDateString(), //
结束时间字段值
"false" //
婚否字段值
});

(方法部分)

#region WriteXmlByDataSet

                   /// summary

                   /// Xml文件插入一行数据

                   /// /summary

                   /// param name="strXmlPath"xml文件相对路径〈/param

                   /// param name="Columns"〉要插入行的列名数组,如:string[] Columns = {"name","IsMarried"};/param

                   /// param name="ColumnValue"〉要插入行每列的值数组,如:string[] ColumnValue={"kgdiwss","false"};/param

                   /// returns〉成功返回true,否则返回false/returns

                   public static bool WriteXmlByDataSet(string strXmlPath,string[] Columns,string[] ColumnValue)

                   {

                            try

                            {

                   //根据传入的XML路径得到.XSD的路径,两个文件放在同一个目录下                                   string strXsdPath = strXmlPath.Substring(0,strXmlPath.IndexOf(".")) + ".xsd";

                                    

                                     DataSet ds = new DataSet();

                                     //xml架构,关系到列的数据类型

                                     ds.ReadXmlSchema(GetXmlFullPath(strXsdPath));

                                     ds.ReadXml(GetXmlFullPath(strXmlPath));

                                      DataTable dt = ds.Tables[0];

                   //在原来的表格基础上创建新行

                                     DataRow newRow = dt.NewRow();

 

                                     //循环给 一行中的各个列赋值

                                     for(int i=0; i Columns.Length; i++)

                                     {

                                               newRow[Columns[i]] = ColumnValue[i];

                                     }

                                     dt.Rows.Add(newRow);

                                     dt.AcceptChanges();

                                     ds.AcceptChanges();

                                     ds.WriteXml(GetXmlFullPath(strXmlPath)); 

                                     return true;

                            }

                            catch(Exception)

                            {

                                     return false;

                            }

                   }

                   #endregion

二、      修改操作

31更行符合条件的一条Xml记录

(调用部分)

bool b;
b = OperateXmlByDataSet.UpdateXmlRow(
@"xml/xml_xmlDB.xml",
new string[]{"name","peopleNum","description","IsMarried"},
new string[]{"kgdiwss","10","
描述","true"},
"peopleNum",
"3");

(方法部分)

#region UpdateXmlRow

                   /// summary

                   /// 更行符合条件的一条Xml记录

                   /// /summary

                   /// param name="strXmlPath"XML文件路径〈/param

                   /// param name="Columns"〉列名数组〈/param

                   /// param name="ColumnValue"〉列值数组〈/param

                   /// param name="strWhereColumnName"〉条件列名〈/param

                   /// param name="strWhereColumnValue"〉条件列值〈/param

                   /// returns〉〈/returns

                   public static bool UpdateXmlRow(string strXmlPath,string[] Columns,string[] ColumnValue,string strWhereColumnName,string strWhereColumnValue)

                   {

                            try

                            {

                                     //同上一方法

                                     string strXsdPath = strXmlPath.Substring(0,strXmlPath.IndexOf(".")) + ".xsd";

                                    

                                     DataSet ds = new DataSet();

                                      //xml架构,关系到列的数据类型

                                     ds.ReadXmlSchema(GetXmlFullPath(strXsdPath));

                                     ds.ReadXml(GetXmlFullPath(strXmlPath));  

 

                                     //先判断行数

                                     if(ds.Tables[0].Rows.Count 0)

                                     {                                            

                                               for(int i=0; i ds.Tables[0].Rows.Count; i++)

                                               {

                                               //如果当前记录为符合Where条件的记录         if(ds.Tables[0].Rows[i][strWhereColumnName].ToString().Trim().Equals(strWhereColumnValue))

                                                        {

                                 //循环给找到行的各列赋新值

                                                                 for(int j=0; j Columns.Length; j++)

                                                                 {      

                                     ds.Tables[0].Rows[i][Columns[j]] = ColumnValue[j];        

                                                                 }

                                                                 //更新DataSet

                                                                 ds.AcceptChanges();

                                                                 //重新写入XML文件

                                                                 ds.WriteXml(GetXmlFullPath(strXmlPath));

                                                                 return true;

                                                        }

                                               }                                            

                                              

                                     }                                            

                                     return false;

                            }

                            catch(Exception)

                            {

                                     return false;

                            }

                   }

                   #endregion

三、      删除操作

41删除所有行

(调用部分)

 

bool b;
b = OperateXmlByDataSet.DeleteXmlRows(
@"xml/xml_xmlDB.xml", //XML
文件路径
"name", //
条件列
new string[]{
"
1", //条件值1
"
2", //条件值2
"
3" //条件值3
});

(方法部分)

 

#region DeleteXmlAllRows

                   /// summary

                   /// 删除所有行

                   /// /summary

                   /// param name="strXmlPath"XML路径〈/param

                   /// returns〉〈/returns

                   public static bool DeleteXmlAllRows(string strXmlPath)

                   {

                            try

                            {

                                     DataSet ds = new DataSet();

                                     ds.ReadXml(GetXmlFullPath(strXmlPath));

                                     //如果记录条数大于0

                                     if(ds.Tables[0].Rows.Count 0)

                                     {

                                               //移除所有记录

                                               ds.Tables[0].Rows.Clear();

                                     }

                                     //重新写入,这时XML文件中就只剩根节点了

                                     ds.WriteXml(GetXmlFullPath(strXmlPath));                             

                                     return true;

                            }

                            catch(Exception)

                            {

                                     return false;

                            }                

                   }

                   #endregion

42删除指定Index值的行的方法为:

                   #region DeleteXmlRowByIndex

                   /// summary

                   /// 通过删除DataSetiDeleteRow这一行,然后重写Xml以实现删除指定行

                   /// /summary

                   /// param name="strXmlPath"〉〈/param

                   /// param name="iDeleteRow"〉要删除的行在DataSet中的Index值〈/param

                   public static bool DeleteXmlRowByIndex(string strXmlPath,int iDeleteRow)

                   {

                            try

                            {

                                     DataSet ds = new DataSet();

                                     ds.ReadXml(GetXmlFullPath(strXmlPath));

                                     if(ds.Tables[0].Rows.Count 0)

                                     {

                                               //删除符号条件的行

                                               ds.Tables[0].Rows[iDeleteRow].Delete();

                                     }

                                     ds.WriteXml(GetXmlFullPath(strXmlPath));

                                     return true;

                            }

                            catch(Exception)

                            {

                                     return false;

                            }                         

                   }

                   #endregion

43删除符合条件的多行:

 

        #region DeleteXmlRows
                                         /// summary
                                         /// 删除strColumn列中值为ColumnValue的行
                                         /// /summary
                                         /// param name="strXmlPath"xml相对路径〈/param
                                         /// param name="strColumn"〉列名〈/param
                                         /// param name="ColumnValue"strColumn列中值为ColumnValue的行均会被删除〈/param
                                         /// returns〉〈/returns
                                         public static bool DeleteXmlRows(string strXmlPath,string strColumn,string[] ColumnValue)
                                         {
                                                             try
                                                             {
                                                                                 DataSet ds = new DataSet();
                                                                                 ds.ReadXml(GetXmlFullPath(strXmlPath));
 
                                                                                 //先判断行数
                                                                                 if(ds.Tables[0].Rows.Count  0)
                                                                                 {
                                                                                                      //判断行多还是删除的值多,多的for循环放在里面
                                                                                                      if(ColumnValue.Length  ds.Tables[0].Rows.Count)
                                                                                                      {
                                                                                                                          for(int i=0; i  ds.Tables[0].Rows.Count; i++)
                                                                                                                          {                                                                                                                             
                                                                                                                                              for(int j=0; j  ColumnValue.Length; j++)
                                                                                                                                              {                                                                                                         
                                                                                                                                                      //找到符合条件的行                    if(ds.Tables[0].Rows[i][strColumn].ToString().Trim().Equals(ColumnValue[j]))
                                                                                                                                                                   {
                                          //删除行
                                                                                                                                                                                       ds.Tables[0].Rows[i].Delete();
                                                                                                                                                                   }
                                                                                                                                              }
                                                                                                                          }
                                                                                                      }
                                                                                                      else
                                                                                                      {
                                                                                                                          for(int j=0; j  ColumnValue.Length; j++)
                                                                                                                          {                                                                                                                             
                                                                                                                                              for(int i=0; i  ds.Tables[0].Rows.Count; i++)
                                                                                                                                              {                                                                                                                                                                      
                                                                                                                                                 //找到符合条件的行                    if(ds.Tables[0].Rows[i][strColumn].ToString().Trim().Equals(ColumnValue[j]))
                                                                                                                                                                   {//删除行
                                                                                                                                                                                       ds.Tables[0].Rows[i].Delete();
                                                                                                                                                                   }
                                                                                                                                              }
                                                                                                                          }                                                                                                                        
                                                                                                      }                                       
                                                                                                      ds.WriteXml(GetXmlFullPath(strXmlPath));
                                                                                 }                                                                                
                                                                                 return true;                       
                                                             }
                                                             catch(Exception)
                                                             {
                                                                                 return false;
                                                             }                                                           
                                         }
                                         #endregion

转载注明地址:http://www.chengxuyuans.com/xml/22133.html

推荐文章