XML _ Operations

Source: Internet
Author: User

Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Using system. IO;
Using system. xml;

Namespace xmldom
{
/// <Summary>
/// Summary of webform1.
/// </Summary>
Public class webform1: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. textbox textbox1;
Protected system. Web. UI. webcontrols. textbox textbox2;
Protected system. Web. UI. webcontrols. textbox textbox3;
Protected system. Web. UI. webcontrols. Button button1;
Protected system. Web. UI. webcontrols. DataGrid datagrid1;
 
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
Readxml ();
}

Private void readxml ()
{
String url = server. mappath ("user. xml"); // obtain the XML file in the current folder
Streamreader sread = new streamreader (URL, system. text. encoding. getencoding ("gb2312"); // read characters from the byte stream with a specific encoding. garbled characters cannot be obtained only after being converted to gb2312.
Xmldatadocument datadoc = new xmldatadocument (); // operate XML documents
Datadoc. dataset. readxml (sread); // Save the read byte stream to dataset.
This. datagrid1.datasource = datadoc. dataset. Tables [0]. defaultview;
Datagrid1.datakeyfield = "username"; // create an index with Username
This. datagrid1.databind ();
Datadoc = NULL; // clear the XML data operation
Sread. Close (); // close byte stream reading

}

# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. Maid + = new system. Web. UI. webcontrols. Fig cancelcommand );
This. Maid + = new system. Web. UI. webcontrols. Maid (this. Maid editcommand );
This. Maid + = new system. Web. UI. webcontrols. Maid (this. Maid updatecommand );
This. Maid + = new system. Web. UI. webcontrols. Maid (this. Maid deletecommand );
This. button1.click + = new system. eventhandler (this. button#click );
This. Load + = new system. eventhandler (this. page_load );

}
# Endregion

Private void datagrid1_deletecommand (Object source, system. Web. UI. webcontrols. datagridcommandeventargs E)
{
String url = server. mappath ("user. xml ");
String str_username = maid [E. Item. itemindex]. tostring ();
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (URL); // load the XML file
Xmlnode root = xmldoc. selectsinglenode ("userinfo"); // find the userinfo Node
Xmlnodelist xnl = root. childnodes; // obtain the subnode under userinfo
For (INT I = 0; I <xnl. Count; I ++)
{
Xmlnodelist xnl11 = xnl. Item (I). selectnodes ("username"); // find the node set under Username
Xmlelement Xe = (xmlelement) xnl11.item (0); // elements under the username Node
If (Xe. innertext = str_username) // gets the serial values of nodes and all subsets.
{

Root. removechild (Xe. parentnode); // remove the finger-top node
// Xe. parentnode. removechild (xe );
// Xnl. Item (I). removechild (xe );
// If (I <xnl. Count) I = I-1;
}
}
Xmldoc. Save (URL );
Readxml ();

}

Private void button#click (Object sender, system. eventargs E)
{
String url = server. mappath ("user. xml ");
String str_username = This. textbox1.text. tostring ();
String str_password = This. textbox2.text. tostring ();
String str_study = This. textbox3.text. tostring ();
Try
{
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (URL); // load the XML document
Xmlnode root = xmldoc. selectsinglenode ("userinfo"); // find the First Matching xmlnode
Xmlelement xe1 = xmldoc. createelement ("datarow"); // create the first element of the first node

Xmlelement xesub1 = xmldoc. createelement ("username"); // create attributes in the first element of the first node
Xesub1.innertext = str_username; // set the text node
Xe1.appendchild (xesub1); // Add the specified node to the end of the node's subnode list
Xmlelement xesub2 = xmldoc. createelement ("password ");
Xesub2.innertext = str_password;
Xe1.appendchild (xesub2 );
Xmlelement xesub3 = xmldoc. createelement ("study ");
Xesub3.innertext = str_study;
Xe1.appendchild (xesub3 );

Root. appendchild (xe1 );
Xmldoc. Save (URL); // Save the XML file

Readxml ();
}
Catch (exception ex)
{
Response. Write (ex. tostring ());
}

}

Private void datagrid1_editcommand (Object source, system. Web. UI. webcontrols. datagridcommandeventargs E)
{
This. datagrid1.edititemindex = E. Item. itemindex;
Readxml ();
}

Private void datagrid1_cancelcommand (Object source, system. Web. UI. webcontrols. datagridcommandeventargs E)
{
This. Maid =-1;
Readxml ();
}

Private void datagrid1_updatecommand (Object source, system. Web. UI. webcontrols. datagridcommandeventargs E)
{
String url = server. mappath ("user. xml ");
String str_username = maid [E. Item. itemindex]. tostring ();

Textbox txt_username = (textbox) (E. Item. cells [2]. controls [0]);
Textbox txt_password = (textbox) (E. Item. cells [3]. controls [0]);
Textbox drp_study = (textbox) (E. Item. cells [4]. controls [0]);

Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (URL); // load the XML file
Xmlnode root = xmldoc. selectsinglenode ("userinfo"); // find the userinfo Node
Xmlnodelist xnl = root. childnodes; // obtain the subnode under userinfo
For (INT I = 0; I <xnl. Count; I ++)
{
Xmlnodelist xnl11 = xnl. Item (I). selectnodes ("username"); // find the node set under Username
Xmlelement Xe = (xmlelement) xnl11.item (0); // elements under the username Node
If (Xe. innertext = str_username) // gets the serial values of nodes and all subsets.
{
Xe. innertext = txt_username.text;
Xe. parentnode. childnodes. Item (1). innertext = txt_password.text;
Xe. parentnode. childnodes. Item (2). innertext = drp_study.text;
}

}
Xmldoc. Save (URL );
This. Maid =-1;
Readxml ();

}
}
}

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.