<XmlDataSet>
  <UserInfo>
    <Id>1</Id>
    <">

GridView&DetailsView对XML文件增删改

80酷酷网    80kuku.com

  xml

XML文件:
<?xml version="1.0" standalone="yes"?>
<XmlDataSet>
  <UserInfo>
    <Id>1</Id>
    <Name>赵AX</Name>
  </UserInfo>
  <UserInfo>
    <Id>2</Id>
    <Name>钱BB</Name>
  </UserInfo>
  <UserInfo>
    <Id>3</Id>
    <Name>孙CC</Name>
  </UserInfo>
  <UserInfo>
    <Id>4</Id>
    <Name>李DY</Name>
  </UserInfo>
  <UserInfo>
    <Id>5</Id>
    <Name>周EF</Name>
  </UserInfo>
  <UserInfo>
    <Id>6</Id>
    <Name>吴FF</Name>
  </UserInfo>
  <UserInfo>
    <Id>7</Id>
    <Name>郑GG</Name>
  </UserInfo>
</XmlDataSet>前台代码:
<% Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DeleteMethod="DeleteDataSet"
            InsertMethod="InsertDataSet" SelectMethod="GetDataSet" TypeName="UserInfoTable"
            UpdateMethod="UpdateDataSet">
            <DeleteParameters>
                <asp:Parameter Name="Id" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="Id" Type="Int32" />
                <asp:Parameter Name="Name" Type="String" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="Name" Type="String" />
            </InsertParameters>
        </asp:ObjectDataSource>
         </div>
        <asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource1">
            <Columns>
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
            </Columns>
        </asp:GridView>
        <asp:DetailsView ID="DetailsView1" runat="server" DataSourceID="ObjectDataSource1"
            Height="81px" Width="125px" AllowPaging="True">
            <Fields>
                <asp:CommandField ShowInsertButton="True" />
            </Fields>
        </asp:DetailsView>
    </form>
</body>
</html>后台(UserInfoTable.cs)代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Globalization;

/// <summary>
/// 操作XML文件
/// </summary>
public class UserInfoTable : IDisposable
{
    /// <summary>
    /// XML文件路径
    /// </summary>
    private static string strXmlFile = "~/App_Data/UserInfo.xml";
    /// <summary>
    /// DataSet对象
    /// </summary>
    private DataSet dataset = null;
    /// <summary>
    /// XML文件相对路径
    /// </summary>
    private string strFilePath = null;

    /// <summary>
    /// 构造函数
    /// </summary>
    public UserInfoTable()
    {
        // 实例化DataSet
        dataset = new DataSet();
        // 不依赖于区域性
        dataset.Locale = CultureInfo.InvariantCulture;
        // 取得XML文件虚拟路径
        strFilePath = HttpContext.Current.Server.MapPath(strXmlFile);
        // 读取XML文件
        dataset.ReadXml(strFilePath);
    }

    /// <summary>
    /// 资源释放
    /// </summary>
    public void Dispose()
    {
        // 关闭DataSet
        dataset.Dispose();
        GC.SuppressFinalize(this);
    }

    /// <summary>
    /// 检索数据
    /// </summary>
    /// <returns>DataSet对象</returns>
    public DataSet GetDataSet()
    {
        // 返回DataSet对象
        return dataset;
    }
    /// <summary>
    /// 更新数据
    /// </summary>
    /// <param name="nID">Id</param>
    /// <param name="strName">Name</param>
    /// <returns>更新是否成功</returns>
    public void UpdateDataSet(int Id, string Name)
    {
        try
        {
            // 选择节点条件
            string strFilter = "Id=" + Id.ToString(CultureInfo.InvariantCulture);
            // 记录取得的行的集合
            DataRow[] rows = dataset.Tables[0].Select(strFilter);
            // 如果取到记录
            if (rows.Length > 0)
            {
                // 更新数据
                rows[0]["Name"] = Name;
                // 保存数据
                Save();
            }
        }
        catch (Exception)
        {
        }
    }

    /// <summary>
    /// 删除一条数据
    /// </summary>
    /// <param name="nID">ID</param>
    /// <returns>是否删除成功</returns>
    public void DeleteDataSet(int Id)
    {

        try
        {
            // 选择节点条件
            string strFilter = "Id=" + Id.ToString(CultureInfo.InvariantCulture);
            // 记录取得的行的集合
            DataRow[] rows = dataset.Tables[0].Select(strFilter);
            // 如果取到记录
            if (rows.Length > 0)
            {
                // 删除数据
                rows[0].Delete();
                // 保存数据
                Save();
            }
        }
        catch (Exception)
        {
        }
    }

    /// <summary>
    /// 插入数据
    /// </summary>
    /// <param name="strName">姓名</param>
    /// <returns>是否插入成功</returns>
    public void InsertDataSet(int Id, string Name)
    {
        DataRow row = dataset.Tables[0].NewRow();
        row["Id"] = Id;
        row["Name"] = Name;
        dataset.Tables[0].Rows.Add(row);
        Save();
    }

    /// <summary>
    /// 保存XML文档
    /// </summary>
    private void Save()
    {
        // 写入XML文档
        dataset.WriteXml(strFilePath, XmlWriteMode.IgnoreSchema);
    }
}


 



分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: