你的位置:首页 > ASP.net教程

[ASP.net教程].net xml 增删改查基础复习及干货分享


今天做做项目时,有一个需求需要用到一些固定的文本数据,觉得将这些需要存储的信息直接写在代码里很不友好,放在数据库中存储又觉得不够方便,自然就想到了使用

C#操作

demo示例代码:

<?version="1.0" encoding="utf-8"><info>  <userinfo>   <name>花哥</name>   <age>26</age>   <email>921442206@qq.com</email>   <website>51chiheng.com</website> </userinfo></info>

增加:


删除操作:


 

修改操作:


 

查询操作:

//下面的代码开始是用页面编码的方式实现的

 

最后分享一点干货,聪明的程序员会学会站在巨人的肩膀上去眺望远方,而不是一直闷头苦干的造轮胎,感谢苏飞大神!

/// <summary>/// 好东西分享: /// 类说明:【感谢苏飞大侠分享的好东西】/// </summary>using System;using System.Data;using System.IO;using System.namespace DotNet.Utilities{  public class #region 构造函数    public public string strPath)    {      this._ strPath;    }    #endregion     #region 公有属性    private string _public string get { return this._#endregion     #region 私有方法    /// <summary>    /// 导入/// </summary>    /// <param name="</param>    private string  = new try      {        string filename = AppDomain.CurrentDomain.BaseDirectory.ToString() + if (File.Exists(filename)) catch (Exception e)      { }      return /// <summary>    /// 导入/// </summary>    /// <param name="</param>    private static string strPath)    {      = new try      {        string filename = AppDomain.CurrentDomain.BaseDirectory.ToString() + strPath;        if (File.Exists(filename)) catch (Exception e)      { }      return /// <summary>    /// 返回完整路径    /// </summary>    /// <param name="strPath"></param>    private static string Getstring strPath)    {      if (strPath.IndexOf(":") > 0)      {        return strPath;      }      else      {        return System.Web.HttpContext.Current.Server.MapPath(strPath);      }    }    #endregion     #region 读取数据    /// <summary>    /// 读取指定节点的数据    /// </summary>    /// <param name="node">节点</param>    /// 使用示列:    /// ///     public string Read(string node)    {      string value = "";      try      {        = = doc.SelectSingleNode(node);        value = xn.InnerText;      }      catch { }      return value;    }     /// <summary>    /// 读取指定路径和节点的串联值    /// </summary>    /// <param name="path">路径</param>    /// <param name="node">节点</param>    /// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param>    /// 使用示列:    /// ///     public static string Read(string path, string node)    {      string value = "";      try      {        = = doc.SelectSingleNode(node);        value = xn.InnerText;      }      catch { }      return value;    }     /// <summary>    /// 读取指定路径和节点的属性值    /// </summary>    /// <param name="path">路径</param>    /// <param name="node">节点</param>    /// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param>    /// 使用示列:    /// ///     public static string Read(string path, string node, string attribute)    {      string value = "";      try      {        = = doc.SelectSingleNode(node);        value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value);      }      catch { }      return value;    }     /// <summary>    /// 获取某一节点的所有孩子节点的值    /// </summary>    /// <param name="node">要查询的节点</param>    public string[] ReadAllChildallValue(string node)    {      int i = 0;      string[] str = { };      = = doc.SelectSingleNode(node);      = xn.ChildNodes; //得到该节点的子节点      if (nodelist.Count > 0)      {        str = new string[nodelist.Count];        foreach (in nodelist)//读元素值        {          str[i] = el.Value;          i++;        }      }      return str;    }     /// <summary>    /// 获取某一节点的所有孩子节点的值    /// </summary>    /// <param name="node">要查询的节点</param>    public string node)    {      = = doc.SelectSingleNode(node);      = xn.ChildNodes; //得到该节点的子节点      return nodelist;    }     /// <summary>     /// 读取/// </summary>    /// <param name="strWhere">筛选条件,如:"name='kgdiwss'"</param>    /// <param name="strSort"> 排序条件,如:"Id desc"</param>    public DataView GetDataViewBystring strWhere, string strSort)    {      try      {        string this.string filename = AppDomain.CurrentDomain.BaseDirectory.ToString() + = new DataSet();        ds.Read= new DataView(ds.Tables[0]); //创建DataView来完成排序或筛选操作         if (strSort != null)        {          dv.Sort = strSort; //对DataView中的记录进行排序        }        if (strWhere != null)        {          dv.RowFilter = strWhere; //对DataView中的记录进行筛选,找到我们想要的记录        }        return dv;      }      catch (Exception)      {        return null;      }    }     /// <summary>    /// 读取/// </summary>    /// <param name="str</param>    public DataSet GetDataSetBystring strtry      {        DataSet ds = new DataSet();        ds.Readif (ds.Tables.Count > 0)        {          return ds;        }        return null;      }      catch (Exception)      {        return null;      }    }    #endregion     #region 插入数据    /// <summary>    /// 插入数据    /// </summary>    /// <param name="path">路径</param>    /// <param name="node">节点</param>    /// <param name="element">元素名,非空时插入新元素,否则在该元素中插入属性</param>    /// <param name="attribute">属性名,非空时插入该元素属性值,否则插入元素值</param>    /// <param name="value">值</param>    /// 使用示列:    /// /// ///     public static void Insert(string path, string node, string element, string attribute, string value)    {      try      {        = new + path);        = doc.SelectSingleNode(node);        if (element.Equals(""))        {          if (!attribute.Equals(""))          {            = (else        {          = doc.CreateElement(element);          if (attribute.Equals(""))            xe.InnerText = value;          else            xe.SetAttribute(attribute, value);          xn.AppendChild(xe);        }        doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path);      }      catch { }    }     /// <summary>    /// 插入数据    /// </summary>    /// <param name="path">路径</param>    /// <param name="node">节点</param>    /// <param name="element">元素名,非空时插入新元素,否则在该元素中插入属性</param>    /// <param name="strList">由</param>    public static void Insert(string path, string node, string element, string[][] strList)    {      try      {        = new + path);        = doc.SelectSingleNode(node);        = doc.CreateElement(element);        string strAttribute = "";        string strValue = "";        for (int i = 0; i < strList.Length; i++)        {          for (int j = 0; j < strList[i].Length; j++)          {            if (j == 0)              strAttribute = strList[i][j];            else              strValue = strList[i][j];          }          if (strAttribute.Equals(""))            xe.InnerText = strValue;          else            xe.SetAttribute(strAttribute, strValue);        }        xn.AppendChild(xe);        doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path);      }      catch { }    }     /// <summary>    /// 插入一行数据    /// </summary>    /// <param name="str</param>    /// <param name="Columns">要插入行的列名数组,如:string[] Columns = {"name","IsMarried"};</param>    /// <param name="ColumnValue">要插入行每列的值数组,如:string[] ColumnValue={"</param>    /// <returns>成功返回true,否则返回false</returns>    public static bool Writestring strstring[] Columns, string[] ColumnValue)    {      try      {        //根据传入的        string strXsdPath = str0, str".")) + ".xsd";        DataSet ds = new DataSet();        ds.Read//读        ds.Read= 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.Writereturn true;      }      catch (Exception)      {        return false;      }    }    #endregion     #region 修改数据    /// <summary>    /// 修改指定节点的数据    /// </summary>    /// <param name="node">节点</param>    /// <param name="value">值</param>    public void Update(string node, string value)    {      try      {        = = doc.SelectSingleNode(node);        xn.InnerText = value;        doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + catch { }    }     /// <summary>    /// 修改指定节点的数据    /// </summary>    /// <param name="path">路径</param>    /// <param name="node">节点</param>    /// <param name="value">值</param>    /// 使用示列:    /// ///     public static void Update(string path, string node, string value)    {      try      {        = = doc.SelectSingleNode(node);        xn.InnerText = value;        doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path);      }      catch { }    }     /// <summary>    /// 修改指定节点的属性值(静态)    /// </summary>    /// <param name="path">路径</param>    /// <param name="node">节点</param>    /// <param name="attribute">属性名,非空时修改该节点属性值,否则修改节点值</param>    /// <param name="value">值</param>    /// 使用示列:    /// ///     public static void Update(string path, string node, string attribute, string value)    {      try      {        = = doc.SelectSingleNode(node);        = (if (attribute.Equals(""))          xe.InnerText = value;        else          xe.SetAttribute(attribute, value);        doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path);      }      catch { }    }     /// <summary>    /// 更改符合条件的一条记录    /// </summary>    /// <param name="str</param>    /// <param name="Columns">列名数组</param>    /// <param name="ColumnValue">列值数组</param>    /// <param name="strWhereColumnName">条件列名</param>    /// <param name="strWhereColumnValue">条件列值</param>    public static bool Updatestring strstring[] Columns, string[] ColumnValue, string strWhereColumnName, string strWhereColumnValue)    {      try      {        string strXsdPath = str0, str".")) + ".xsd";        DataSet ds = new DataSet();        ds.Read//读        ds.Read//先判断行数        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];              }              ds.AcceptChanges();           //更新DataSet              ds.Write//重新写入              return true;            }          }         }        return false;      }      catch (Exception)      {        return false;      }    }    #endregion     #region 删除数据    /// <summary>    /// 删除节点值    /// </summary>    /// <param name="path">路径</param>    /// <param name="node">节点</param>    /// <param name="attribute">属性名,非空时删除该节点属性值,否则删除节点值</param>    /// <param name="value">值</param>    /// 使用示列:    /// ///     public static void Delete(string path, string node)    {      try      {        = = doc.SelectSingleNode(node);        xn.ParentNode.RemoveChild(xn);        doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path);      }      catch { }    }     /// <summary>    /// 删除数据    /// </summary>    /// <param name="path">路径</param>    /// <param name="node">节点</param>    /// <param name="attribute">属性名,非空时删除该节点属性值,否则删除节点值</param>    /// <param name="value">值</param>    /// 使用示列:    /// ///     public static void Delete(string path, string node, string attribute)    {      try      {        = = doc.SelectSingleNode(node);        = (if (attribute.Equals(""))          xn.ParentNode.RemoveChild(xn);        else          xe.RemoveAttribute(attribute);        doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path);      }      catch { }    }     /// <summary>    /// 删除所有行    /// </summary>    /// <param name="str</param>    public static bool Deletestring strtry      {        DataSet ds = new DataSet();        ds.Readif (ds.Tables[0].Rows.Count > 0)        {          ds.Tables[0].Rows.Clear();        }        ds.Writereturn true;      }      catch (Exception)      {        return false;      }    }     /// <summary>    /// 通过删除DataSet中指定索引行,重写/// </summary>    /// <param name="iDeleteRow">要删除的行在DataSet中的Index值</param>    public static bool Deletestring strint iDeleteRow)    {      try      {        DataSet ds = new DataSet();        ds.Readif (ds.Tables[0].Rows.Count > 0)        {          ds.Tables[0].Rows[iDeleteRow].Delete();        }        ds.Writereturn true;      }      catch (Exception)      {        return false;      }    }     /// <summary>    /// 删除指定列中指定值的行    /// </summary>    /// <param name="str</param>    /// <param name="strColumn">列名</param>    /// <param name="ColumnValue">指定值</param>    public static bool Deletestring strstring strColumn, string[] ColumnValue)    {      try      {        DataSet ds = new DataSet();        ds.Readif (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.Writereturn true;      }      catch (Exception)      {        return false;      }    }    #endregion  }}

View Code