Use dataset to realize the addition and division of XML data records

Source: Internet
Author: User
SQL and Oracle are generally used for data processing. Here, this article mainly uses C # to perform some data manipulation operations on XML, and shows the same functions as SQL and other data manipulation operations. Create a webpage in C #. Add two DataGrid controls, five textbox controls, and three button controls.
The IDS of the two DataGrid controls are DG and DG1, the DG is used to display the data in XML. to meet future requirements, you must add a custom template templatecolumn to define a checkbox control with the ID ck, four of the five textbox controls are used to add data to the current XML file, and one is used to check the XML file. The three buttons are used for searching, adding, and deleting. The data records with the ID Search, add, Del, and books. XML are 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. Reference System. IO in C #, and define a static dataset ds, static dataview DV in class. Then, compile a function to display 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;
DG. datasource = DV;
DG. databind ();
}
Reference Function in page_load:
If (! Ispostback)
{
Dg_bind ();
}

2. Add a data volume to 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 number of rows. If the number of rows is simple, check the rows for DV. The rowfilter method of DV is used. Example:
private void search (Object sender, system. eventargs e)
{< br> DV. sort = "bookname";
DV. rowfilter = "bookname like '%" + bookname. text. trim () + "% '";
dg1.datasource = DV;
dg1.databind ();
}

4. Divide data into XML files, in this case, we will use the templatecolumn function of the checkbox control added in the DG. First, we will use a statement to determine whether the checkbox is selected, and then divide the selected columns. and load it into the XML file. The typical proxies are as follows ﹕
Private void del_click (Object sender, system. eventargs E)
{
For (INT I = 0; I <this. DG. Items. Count; I ++)
{
Checkbox ck = (checkbox) This. DG. items [I]. findcontrol ("ck ");
If (CK. Checked)
{
DS. Tables [0]. defaultview. Sort = "bookid ";
Int J = Ds. Tables [0]. defaultview. Find (this. DG. items [I]. cells [1]. Text );
DS. Tables [0]. defaultview. Delete (j );
DG. 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 XML query, addition, and Division function using C # is just like this, which is similar to operations on SQL, Oracle, and other data. there is no difference in the same method, and I hope it will help you a little.

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.