Using a dataset to actually add XML data ﹑ Delete

Source: Internet
Author: User
Tags sort trim
XML for the operation of the data library, the general use of sql﹑oracle and so on. Here, this article is mainly to use C # to do some data on XML simple operations, the actual function of the SQL and other data library operations. First create a new Web page in C #. Add two DataGrid controls and five TextBox controls and three button controls
The ID of the two DataGrid controls is DG﹑DG1, where DG is used to display the data in the XML, to add a custom template TemplateColumn, to define a CheckBox control,id as CK, Five TextBox controls four of them are for the fact that you want to add data to XML it is a function of ﹑ in XML data. Three button with the ﹑ add ﹑ delete,id for search﹑add﹑del,books.xml data is as follows:
<Books>
<Books>
<BookId>102</BookId>
<BookName>C++</BookName>
<BookPrice>39</BookPrice>
<Supplier>Phei</Supplier>
</Books>
<Books>
<BookId>103</BookId>
<BookName>VB</BookName>
<BookPrice>52.2</BookPrice>
<Supplier>Pe</Supplier>
</Books>
<Books>
<BookId>105</BookId>
<bookname>build c++</bookname>
<BookPrice>123</BookPrice>
<Supplier>ww</Supplier>
</Books>
</Books>
1﹑ refers to System.IO in C # and defines the two Static datasets Ds,static DataView dv. Then write a function that shows the XML data:
private void Dg_bind ()
{
FileStream fs=new FileStream (Server.MapPath (". \") + "Xml\\books.xml", FileMode.Open,FileAccess.Read);
StreamReader reader=new StreamReader (FS);
Ds=new DataSet ();
Ds. READXML (reader);
Fs. Close ();
Dv=new DataView ();
Dv=ds. Tables[0]. DefaultView;
Cgi DATASOURCE=DV;
Cgi DataBind ();
}
To reference a function in Page_Load:
if (! IsPostBack)
{
Dg_bind ();
}

2﹑ add data to the XML:
private void Add_click (object sender, System.EventArgs e)
{
FileStream fs=new FileStream (Server.MapPath (". \") + "Xml\\books.xml", FileMode.Open,FileAccess.Read);
StreamReader reader=new StreamReader (FS);
Ds=new DataSet ();
Ds. READXML (reader);
Fs. Close ();
DataRow Dr=ds. Tables[0]. NewRow ();
dr["BookName"]=book.text.trim ();
dr["BookID"]=bid.text.trim ();
dr["Bookprice"]=price.text.trim ();
dr["Supplier"]=supp.text.trim ();
Ds. Tables[0]. Rows.Add (DR);
Ds. AcceptChanges ();

Fs=new FileStream (Server.MapPath (". \") + "Xml\\books.xml", filemode.create,fileaccess.write| FileAccess.Read);
StreamWriter Writer=new StreamWriter (FS);
Ds. WriteXml (writer);
Writer. Close ();
Fs. Close ();
Dg_bind ();
}

3﹑ Check the data, check the data is very simple, to DV check is OK, mainly using DV RowFilter method. The codes are as follows:
private void Search (object sender, System.EventArgs e)
{
Dv. Sort= "BookName";
Dv. Rowfilter= "BookName like '%" +bookname.text.trim () + "%";
DG1. DATASOURCE=DV;
DG1. DataBind ();
}

The 4﹑ data is erased and written into an XML file, in this case, we're going to use the TemplateColumn effect of the CheckBox control added to DG, first using a loop to determine whether a checkbox is selected, and then delete the column in the selection and write it into an XML file. The code of the body is as follows:
private void Del_click (object sender, System.EventArgs e)
{
for (int i=0;i<this. Cgi items.count;i++)
{
CheckBox ck= (checkbox) this. Cgi Items[i]. FindControl ("CK");
if (CK. Checked)
{
Ds. Tables[0]. Defaultview.sort= "BookID";
int J=ds. Tables[0]. Defaultview.find (this. Cgi Items[i]. CELLS[1]. Text);
Ds. Tables[0]. Defaultview.delete (j);
Cgi Datasource=ds. Tables[0]. DefaultView;
Ds. AcceptChanges ();
FileStream fs=new FileStream (Server.MapPath (". \") + "Xml\\books.xml", filemode.create,fileaccess.write| FileAccess.Read);
StreamWriter Writer=new StreamWriter (FS);
Ds. WriteXml (writer);
Writer. Close ();
Fs. Close ();
}
}
Dg_bind ();
}

5﹑ a simple search for XML using C # ﹑ Add the ﹑ delete function, which is the same as the operation of the sql﹑oracle and other data library methods, there is no difference, I hope to help you a bit.




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.