XML read/write

Source: Internet
Author: User

Private string filename = httpcontext. Current. server. mappath ("~ /Student. xml ");

Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
Getallstudent ();
}
}

Private void getallstudent ()
{
Ddlstudent. Items. Clear ();
Xmldocument Doc = new xmldocument ();
Doc. Load (filename );

Xmlnodelist nodes = Doc. getelementsbytagname ("name ");
Foreach (xmlelement XML in nodes)
{
Ddlstudent. Items. Add (XML. innertext );
}

}


Protected void btnname_click (Object sender, eventargs E)
{
Selstudent ();

}

Private void selstudent ()
{
Xmldocument Doc = new xmldocument ();
Doc. Load (filename );

String name = ddlstudent. text;

Xmlnode node = Doc. selectsinglenode ("// student [name = '" + name + "']");

If (node! = NULL)
{
This.txt No. Text = node. attributes ["ID"]. value;
This.txt name. Text = node. childnodes [0]. innertext;
This.txt age. Text = node. childnodes [2]. innertext;
This.txt sex. Text = node. childnodes [1]. innertext;
}
}


Protected void btnadd_click (Object sender, eventargs E)
{
Xmldocument Doc = new xmldocument ();
Doc. Load (filename );

Xmlnode node = Doc. documentelement;

Xmlelement Stu = Doc. createelement ("student ");

Xmlattribute stuid = Doc. createattribute ("ID ");
Stuid. innertext = txtno. text;
Stu. Attributes. append (stuid );

Xmlelement stuname = Doc. createelement ("name ");
Stuname. innertext = txtname. text;
Stu. appendchild (stuname );

Xmlelement stuage = Doc. createelement ("Age ");
Stuage. innertext = txtage. text;
Stu. appendchild (stuage );

Xmlelement stusex = Doc. createelement ("sex ");
Stusex. innertext = txtsex. text;
Stu. appendchild (stusex );
Node. appendchild (Stu );


Scriptmanager. registerstartupscript (btnadd, this. GetType (), "Del", "alert ('added successfully')", true );
Doc. Save (filename );
Getallstudent ();
}
Protected void btnup_click (Object sender, eventargs E)
{
Xmldocument Doc = new xmldocument ();

Doc. Load (filename );

String name = ddlstudent. text;

Xmlnode Stu = Doc. selectsinglenode ("// student [name = '" + name + "']");
If (STU! = NULL)
{
Stu. attributes ["ID"]. innertext = txtno. text;
Stu. childnodes [0]. innertext = txtname. text;
Stu. childnodes [1]. innertext = txtage. text;
Stu. childnodes [2]. innertext = txtsex. text;

Scriptmanager. registerstartupscript (btnup, this. GetType (), "Del", "alert ('updated successfully')", true );
}
Doc. Save (filename );
Getallstudent ();
}
Protected void btndel_click (Object sender, eventargs E)
{

Xmldocument Doc = new xmldocument ();

Doc. Load (filename );

Xmlnode node = Doc. documentelement;

String name = ddlstudent. text;

Xmlnode Stu = Doc. selectsinglenode ("// student [name = '" + name + "']");
If (STU! = NULL)
{
Node. removechild (Stu );

Scriptmanager. registerstartupscript (btndel, this. GetType (), "Del", "alert ('deleted successfully')", true );

}
Doc. Save (filename );

Getallstudent ();
}
Protected void btnallstudent_click (Object sender, eventargs E)
{
Dataset DS = new dataset ();
DS. readxml (filename );
Lbltable. Text = Ds. Tables [0]. tablename;
This. grdstudent. datasource = Ds. Tables [0];
This. grdstudent. databind ();

}

Protected void btninsertrow_click (Object sender, eventargs E)
{
Dataset DS = new dataset ();
DS. readxml (filename );
Datatable dt = Ds. Tables [0];
Datarow rs = DT. newrow ();
RS ["ID"] = this.txt No. text;
RS ["name"] = this.txt name. text;
RS ["Age"] = this.txt age. text;
RS ["sex"] = this.txt sex. text;
DT. Rows. Add (RS );

DS. writexml (filename );
Scriptmanager. registerstartupscript (btninsertrow, GetType (), "insert", "alert ('inserted successfully')", true );

Getallstudent ();
}

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.