Use the C # + (DataGrid Control) to edit an XML file

Source: Internet
Author: User

Delete, modify, or add a new record to an XML file.
The DataGrid Control's sortcommand event is used to sort records in XML.

Email: ouyang76.263.net
------------------------------------------
<% @ Page Language = "C #" trace = "true" %>
<% @ Import namespace = "system. Data" %>
<% @ Import namespace = "system. Io" %>
<Script language = "C #" runat = "server">
String xmlfile = "books2.xml", XPath;
Void page_load (Object OBJ, eventargs E)
{
XPath = server. mappath (xmlfile );
If (! Page. ispostback)
{
Dataload ("ISBN ");
}
}
Void dataload (string psort)
{
Dataset DS = new dataset ();
Filestream FS = new filestream (XPath, filemode. Open );
DS. readxml (FS );
If (Ds. Tables. Count = 0)
{
Response. Write ("no record exists in the XML file !!!! ");
FS. Close ();
Response. End ();
}
Trace. Warn ("Number of table records", convert. tostring (Ds. Tables [0]. Rows. Count ));
Datarow DR = Ds. Tables [0]. newrow (); // create a new row
Dr ["ISBN"] = "add ISBN ";
DS. Tables [0]. Rows. insertat (DR, 0); // insert to the position of row 0th
Trace. Warn ("number of tables", convert. tostring (Ds. Tables. Count); // display debugging information in red letters
// Grid1.datasource = Ds. Tables [0]. defaultview;
// Grid1.databind ();
Dataview DV = new dataview (Ds. Tables [0]);
Trace. Warn ("String Length:" + psort, convert. tostring (psort. Length); // sort the string length
If (psort. length> 0)
DV. Sort = psort;
Grid1.datasource = DV;
Grid1.databind ();
FS. Close ();
}
Void grid_sort (Object OBJ, datagridsortcommandeventargs E)
{
If (grid1.edititemindex =-1)
Dataload (E. sortexpression );
Else
Response. Write ("editing cannot be sorted now !! ");
}
Void grid_edit (Object OBJ, datagridcommandeventargs E)
{
Grid1.edititemindex = (INT) E. Item. itemindex;
Show_del ("hide ");
Dataload ("");
}
Void grid_cancel (Object OBJ, datagridcommandeventargs E)
{
Grid1.edititemindex =-1;
Show_del ("show ");
Dataload ("");
}
Void grid_update (Object OBJ, datagridcommandeventargs E)
{
Int numcell = E. Item. cells. Count; // number of cells (E. item is the table row with the current event)
Int currentrow = E. Item. datasetindex;
// Int curr2 = E. Item. itemindex; // It is equivalent to the previous sentence and can contain no (INT)
Trace. Warn ("current updated row number =", convert. tostring (currentrow ));
// Trace. Warn ("2 currently updated row number =", convert. tostring (curr2 ));
Dataset DS = new dataset ();
DS. readxml (XPath); // read the XML mode and data to dataset;
Datarow Dr; // indicates the information of a row in the datatable.
If (currentrow = 0)
Dr = Ds. Tables [0]. newrow ();
Else
Dr = Ds. Tables [0]. Rows [E. Item. datasetindex-1];
String [] STR = {"ISBN", "author", "title", "category", "Comments "};
Int J =-1;
For (INT I = 2; I <numcell; I ++) // skip 1 and 2 columns
{
J = J + 1;
String ctext;
Ctext = (textbox) E. Item. cells [I]. controls [0]). text;
Dr [STR [J] = ctext;
Trace. Warn (convert. tostring (I) + STR [J] + ": Text of each line", ctext );
}
If (currentrow = 0)
{
Response. Write ("Add new record !! ");
DS. Tables [0]. Rows. insertat (DR, 0 );
}
DS. writexml (XPath); // write the XML that represents dataset into the XML file, including data and mode.
Grid1.edititemindex =-1; // The editing page is still blank.
Show_del ("show ");
Dataload ("");
}
Void show_del (string state)
{
String TMP = State;
Switch (TMP)
{
Case "show ":
Grid1.columns [0]. Visible = true;
Break;
Case "hide ":
Grid1.columns [0]. Visible = false;
Break;
Default:
Grid1.columns [0]. Visible = true;
Break; // It must also contain break.
}
}

Void initialize (Object OBJ, datagriditemeventargs e) // note that parameters are different from other functions.
{
// E. Item. cells [0]. Text = "AAAAA ";//
If (E. Item. itemindex = 0) // if it is the first row
{
Linkbutton a0 = new linkbutton ();
A0 = (linkbutton) E. Item. cells [0]. controls [0];
Linkbutton a1 = new linkbutton ();
A1 = (linkbutton) E. Item. cells [1]. controls [0]; // create a linkbutton control in the grid
If (a0.text = "delete ")
A0.text = "";
If (a1.text = "edit ")
A1.text = "[addnew]";
}
}
Void grid_del (Object OBJ, datagridcommandeventargs E)
{
Response. Write ("XX ");
Trace. Warn ("deleting", convert. tostring (E. Item. itemindex); // number of rows in the control
Int curr = E. Item. itemindex;
Dataset DS = new dataset ();
DS. readxml (XPath );
Datarow DR = Ds. Tables [0]. Rows [curr-1]; // a new row is added.
Dr. Delete (); // locate the corresponding data row and delete it.
DS. writexml (XPath );
Grid1.edititemindex =-1;
Dataload ("");

}
</SCRIPT>

<Form runat = "server">
<Asp: DataGrid id = "grid1" runat = "server"
Alternatingitemstyle-backcolor = "# eeeeee"
Headerstyle-backcolor = "lightyellow"
Font-size = "10pt"
Allowsorting = "true"
Onsortcommand = "grid_sort"
Oneditcommand = "grid_edit"
Oncancelcommand = "grid_cancel"
Onupdatecommand = "grid_update"
Onitemcreated = "initialize"
Ondeletecommand = "grid_del"
Bordercolor = "#999999"
>
<Columns>
<Asp: buttoncolumn text = "delete" commandname = "delete"/>
<Asp: editcommandcolumn buttontype = "linkbutton" updatetext = "newer" canceltext = "Remove" edittext = "edit" headertext = ""/>
</Columns>
</ASP: DataGrid>
</Form>
--------------------------------------------------------------------
XML file (File Name: books2.xml)

<? XML version = "1.0" standalone = "yes"?>
<Books>
<Book>
<ISBN> 2e2e2we2we2 </ISBN>
<Author> fefdw </author>
<Title> 2e2eef </title>
<Category> 324tg </Category>
<Comments> r3rrgeqw21 </comments>
</Book>
<Book>
<ISBN> 1234345 </ISBN>
<Author> ssdfdfe </author>
<Title> fgregre </title>
<Category> r4er43trt </Category>
<Comments> r3r3redqeq </comments>
</Book>
<Book>
<ISBN> 1234345 </ISBN>
<Author> ssdfdfe </author>
<Title> fgregre </title>
<Category> r4er43trt </Category>
<Comments> r3r3redqeq </comments>
</Book>
<Book>
<ISBN> 0679757651 </ISBN>
<Author> Tom Peters </author>
<Title> circle of innovation </title>
<Category> marketing </Category>
<Comments> his most recent book is his best by far! </Comments>
</Book>
<Book>
<ISBN> 0884270610 </ISBN>
<Author> Eli goldthrait </author>
<Title> the goal </title>
<Category> management </Category>
<Comments> advocate of theory of constraints as applied to management and optimization. </comments>
</Book>
<Book>
<ISBN> 068485600x </ISBN>
<Author> Jeff Cox, Howard Steven S </author>
<Title> selling the wheel </title>
<Category> management </Category>
<Comments> excellent treatise/novel on the entire sales cycle </comments>
</Book>
<Book>
<ISBN> 0672316498 </ISBN>
<Author> Alan Cooper </author>
<Title> the inmates are running the asylum </title>
<Category> management </Category>
<Comments> the father of Visual Basic and creator of the new art of interaction design-very valuable in designing websites. basically the worlds most cutting edge thinker in user interface design aimed at simplifying software use. </comments>
</Book>
</Books>

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/21aspnet/archive/2004/12/23/226266.aspx

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.