Gridview&detailsview of XML file additions and deletions

Source: Internet
Author: User
Tags object insert string tostring
Xml

XML file:
<?xml version= "1.0" standalone= "yes"?>
<XmlDataSet>
<UserInfo>
<Id>1</Id>
<Name> Zhao Ax</name>
</UserInfo>
<UserInfo>
<Id>2</Id>
<Name> Money bb</name>
</UserInfo>
<UserInfo>
<Id>3</Id>
<Name> Sun Cc</name>
</UserInfo>
<UserInfo>
<Id>4</Id>
<Name> Lee Dy</name>
</UserInfo>
<UserInfo>
<Id>5</Id>
<Name> Week ef</name>
</UserInfo>
<UserInfo>
<Id>6</Id>
<Name> Wu Ff</name>
</UserInfo>
<UserInfo>
<Id>7</Id>
<Name> Zheng Gg</name>
</UserInfo>
</XmlDataSet> Foreground Code:
<%@ 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 ">

<title> Untitled Page </title>
<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>
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>
manipulating XML files
</summary>
public class Userinfotable:idisposable
{
<summary>
XML file path
</summary>
private static string strxmlfile = "~/app_data/userinfo.xml";
<summary>
DataSet object
</summary>
private DataSet = null;
<summary>
XML file relative path
</summary>
private string strFilePath = null;

   ///<summary>
   ///Constructor
    ///</summary>
    public userinfotable ()
    {
        //materialized DataSet
        DataSet = new DataSet ();
       //Does not depend on the culture
        DataSet. Locale = CultureInfo.InvariantCulture;
       //Get XML file virtual path
        strFilePath = HttpContext.Current.Server.MapPath (strxmlfile);
       /Read XML file
        DataSet. READXML (strFilePath);
   }

<summary>
Resource release
</summary>
public void Dispose ()
{
Close DataSet
DataSet. Dispose ();
Gc. SuppressFinalize (this);
}

<summary>
Retrieving data
</summary>
<returns>dataset Objects </returns>
Public DataSet GetDataSet ()
{
Returns a DataSet object
return dataset;
}
<summary>
Update data
</summary>
<param name= "NID" >Id</param>
<param name= "StrName" >Name</param>
<returns> whether the update was successful </returns>
public void UpdateDataSet (int Id, string Name)
{
Try
{
Select node condition
String strfilter = "id=" + id.tostring (CultureInfo.InvariantCulture);
Records the collection of rows obtained
datarow[] rows = DataSet. Tables[0]. Select (strfilter);
If you take a record
If rows. Length > 0)
{
Update data
rows[0]["Name" = name;
Save data
Save ();
}
}
catch (Exception)
{
}
}

<summary>
Delete a piece of data
</summary>
<param name= "NID" >ID</param>
<returns> whether to delete success </returns>
public void Deletedataset (int Id)
{

Try
{
Select node condition
String strfilter = "id=" + id.tostring (CultureInfo.InvariantCulture);
Records the collection of rows obtained
datarow[] rows = DataSet. Tables[0]. Select (strfilter);
If you take a record
If rows. Length > 0)
{
Delete data
Rows[0]. Delete ();
Save data
Save ();
}
}
catch (Exception)
{
}
}

   ///<summary>
   ///Insert Data
    ///</summary>
   ///<param name= "StrName" > Name </param>
    ///<returns> Insert Success </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>
Save XML document
</summary>
private void Save ()
{
Writing to an XML document
DataSet. WriteXml (strFilePath, XmlWriteMode.IgnoreSchema);
}
}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.