Read/write XML (C # example, Asp.net)

Source: Internet
Author: User
ASPX page

<% @ Page Language = "C #" codebehind = "xdoc. aspx. cs" autoeventwireup = "false" inherits = "XML. xdoc" %>
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> xdoc </title>
<Meta content = "Microsoft Visual Studio. NET 7.1" name = "generator">
<Meta content = "C #" name = "code_language">
<Meta content = "JavaScript" name = "vs_defaultclientscript">
<Meta content = "http://schemas.microsoft.com/intellisense/ie5" name = "vs_targetschema">
</Head>
<Body ms_positioning = "gridlayout">
<Form ID = "form1" method = "Post" runat = "server">
<Font face = "">
<Asp: DataGrid id = "DG" style = "Z-INDEX: 101; left: 48px; position: absolute; top:

56px "runat =" server"
Forecolor = "black" borderstyle = "NONE" borderwidth = "1px" bordercolor = "# dedfde"

Backcolor = "white"
Cellpadding = "4" gridlines = "vertical" autogeneratecolumns = "false"

Width = "432px">
<Selecteditemstyle font-bold = "true" forecolor = "white"

Backcolor = "# ce5d5a"> </selecteditemstyle>
<Alternatingitemstyle backcolor = "white"> </alternatingitemstyle>
<Itemstyle backcolor = "# f7f7de"> </itemstyle>
<Headerstyle font-bold = "true" forecolor = "white"

Backcolor = "# 6b696b"> <Footerstyle backcolor = "# cccc99"> </footerstyle>
<Columns>
<Asp: boundcolumn datafield = "name" headertext = "name"> </ASP: boundcolumn>
<Asp: boundcolumn datafield = "city" headertext = "city"> </ASP: boundcolumn>
<Asp: boundcolumn datafield = "email" headertext = "mail location

Address "> </ASP: boundcolumn>
<Asp: boundcolumn datafield = "time" headertext = "time"> </ASP: boundcolumn>
</Columns>
<Pagerstyle horizontalalign = "right" forecolor = "black" backcolor = "# f7f7de"

Mode = "numericpages"> </pagerstyle>
</ASP: DataGrid> <asp: Label id = "label1" style = "Z-INDEX: 102; left: pixel; position:

Absolute; top: 8px "runat =" server"
Width = "336px" borderwidth = "2px" borderstyle = "ridge" font-size = "X-large" font

-Bold = "true" forecolor = "red"> xmldocument class mining </ASP: Label> <asp: dropdownlist id = "DDL" style = "Z-

Index: 103; left: 128px; position: absolute; top: 280px"

Runat = "server"> </ASP: dropdownlist> <asp: Label id = "label2" style = "Z-INDEX: 104; left: 64px;

Position: absolute; top: 280px "runat =" server "> name: </ASP: Label> <asp: button id =" button1"

Style = "Z-INDEX: 105; left: 64px; position: absolute; top: 328px" runat = "server"
Width = "64px" text = "query"> </ASP: button> <asp: Label id = "label3" style = "Z-INDEX:

106; left: 64px; position: absolute; top: 384px "runat =" server "> email address: </ASP: Label> <asp: button

Id = "button2" style = "Z-INDEX: 107; left: 152px; position: absolute; top: 328px" runat = "server"
Width = "64px" text = "delete"> </ASP: button> <asp: button id = "button3" style = "Z-

Index: 108; left: 432px; position: absolute; top: 280px "runat =" server"
Width = "56px" text = "add" Height = "80px"> </ASP: button> <asp: textbox id = "TBN"

Style = "Z-INDEX: 109; left: 152px; position: absolute; top: 384px" runat = "server"
Width = "328px"> </ASP: textbox> <asp: button id = "button4" style = "Z-INDEX: 110;

Left: 64px; position: absolute; top: 416px "runat =" server"
Width = "424px" text = "Modify email address"> </ASP: button>
<Asp: textbox id = "TBNA" style = "Z-INDEX: 111; left: 312px; position: absolute; top:

280px "runat =" server"
Width = "pixel PX"> </ASP: textbox>
<Asp: textbox id = "TBC" style = "Z-INDEX: 112; left: 312px; position: absolute; top:

312px "runat =" server"
Width = "pixel PX" Height = "24px"> </ASP: textbox>
<Asp: textbox id = "tbe" style = "Z-INDEX: 113; left: 312px; position: absolute; top:

344px "runat =" server"
Width = "pixel PX" Height = "24px"> </ASP: textbox>
<Asp: Label id = "label4" style = "Z-INDEX: 114; left: pixel PX; position: absolute; top:

280px "runat =" server "> name: </ASP: Label>
<Asp: Label id = "label5" style = "Z-INDEX: 115; left: pixel PX; position: absolute; top:

312px "runat =" server "> City: </ASP: Label>
<Asp: Label id = "label6" style = "Z-INDEX: 116; left: pixel PX; position: absolute; top:

344px "runat =" server "> Email: </ASP: Label> </font> </form>
</Body>
</Html>

CS files
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;
// Two new namespaces are introduced.
Using system. xml;
Using system. xml. XPath;

Namespace XML
{
/** // <Summary>
/// Summary of xdoc.
/// </Summary>
Public class xdoc: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. Label label1;
Protected system. Web. UI. webcontrols. Label label2;
Protected system. Web. UI. webcontrols. Button button1;
Protected system. Web. UI. webcontrols. Label label3;
Protected system. Web. UI. webcontrols. Button button2;
Protected system. Web. UI. webcontrols. Button button3;
Protected system. Web. UI. webcontrols. DataGrid DG;
Protected system. Web. UI. webcontrols. dropdownlist DDL;
Protected system. Web. UI. webcontrols. textbox TBN;
Protected system. Web. UI. webcontrols. Label label4;
Protected system. Web. UI. webcontrols. Label label5;
Protected system. Web. UI. webcontrols. Label label6;
Protected system. Web. UI. webcontrols. textbox TBNA;
Protected system. Web. UI. webcontrols. textbox TBC;
Protected system. Web. UI. webcontrols. textbox tbe;
Protected system. Web. UI. webcontrols. Button button4;

Private void page_load (Object sender, system. eventargs E)
{// You can see the action name as it is loaded.
If (! Page. ispostback)
{
Fill ();
}
}

// Code generated by the web form designer # code generated by the 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. button1.click + = new system. eventhandler (this. button#click );
This. button2.click + = new system. eventhandler (this. button2_click );
This. button3.click + = new system. eventhandler (this. button3_click );
This. button4.click + = new system. eventhandler (this. button4_click );
This. Load + = new system. eventhandler (this. page_load );

}

Private void fill ()
{// Binding method
Dataset DS = new dataset ();
DS. readxml (server. mappath ("\ XML \ doc. xml "));
DG. datasource = Ds. Tables [0];
DG. databind ();
Xmldocument XD = new xmldocument ();
XD. Load (server. mappath ("\ XML \ doc. xml "));
System. xml. xmlnodelist xnl = XD. getelementsbytagname ("name ");
DDL. Items. Clear ();
For (INT I = 0; I <xnl. Count; I ++)
{
DDL. Items. Add (xnl [I]. innertext );
}
}
Private void button#click (Object sender, system. eventargs E)
{// Query action
Xmldocument XD = new xmldocument ();
XD. Load (server. mappath ("\ XML \ doc. xml "));
TBN. Text = XD. selectsinglenode ("// user

[Name = '"+ DDL. selecteditem. Text +"'] "). childnodes. Item (2). innertext. tostring ();
}
Private void button2_click (Object sender, system. eventargs E)
{// Delete action
Xmldocument xdoc = new xmldocument ();
Xdoc. Load (server. mappath ("\ XML \ doc. xml "));
Xmlnodelist xnl = xdoc. selectsinglenode ("dbguest"). childnodes;

Foreach (xmlnode Xn in xnl)
{
Xmlelement Xe = (xmlelement) xn;
Xmlnodelist node = Xe. getelementsbytagname ("name"); // you can call this operation to find the name row.
If (node. Count> 0)
{
If (node [0]. innertext = DDL. selecteditem. Text) // if the name of the current node and the drop-down list are uploaded

To delete
{
Xe. removeall (); // 111
}
Break;
}

}
Xdoc. Save (server. mappath ("\ XML \ doc. xml "));
Fill ();
}

Private void button3_click (Object sender, system. eventargs E)
{// Add action
String name = This. TBNA. Text. tostring ();
String city = This. TBC. Text. tostring ();
String email = This. tbe. Text. tostring ();

Xmldocument XD = new xmldocument ();
XD. Load (server. mappath ("\ XML \ doc. xml "));
// Find the dbguest Node
Xmlnode xn = XD. selectsinglenode ("dbguest ");
// Create a user Node
Xmlelement xe_u = XD. createelement ("user ");
// Create a name Element
Xmlelement xe_a1 = XD. createelement ("name ");
Xe_a1.innertext = Name; // set the value of this element.
Xe_u.appendchild (xe_a1); // Insert the created element to the Name node.
Xmlelement xe_a2 = XD. createelement ("city ");
Xe_a2.innertext = city;
Xe_u.appendchild (xe_a2 );
Xmlelement xe_a3 = XD. createelement ("email ");
Xe_a3.innertext = Email;
Xe_u.appendchild (xe_a3 );
Xmlelement xe_a4 = XD. createelement ("time ");
Xe_a4.innertext = (datetime. Now). tostring ();
Xe_u.appendchild (xe_a4 );
// Insert the user node to dbguest
XN. appendchild (xe_u );
// Save the XML file
XD. Save (server. mappath ("\ XML \ doc. xml "));
Fill ();
}

Private void button4_click (Object sender, system. eventargs E)
{// Modify email address action
Xmldocument XD = new xmldocument ();
XD. Load (server. mappath ("\ XML \ doc. xml"); // load the XML file
Xmlnodelist xnl = XD. selectsinglenode ("dbguest"). childnodes; // retrieve all subsections of the dbguest Node

Point
Foreach (xmlnode Xn in xnl) // iterate all subnodes
{
Xmlelement Xe = (xmlelement) xn; // converts the child nodes that are iterated to the xmlelement type
Xmlnodelist xnl2 = Xe. getelementsbytagname ("name"); // return data of all name Columns
If (xnl2.count> 0) // if the value is greater than zero,
{
If (xnl2 [0]. innertext = DDL. selecteditem. Text) // If This column has

Data with the same value
{
Xmlnodelist xnl3 = Xe. childnodes; // obtain all the following subnodes.
Foreach (xmlnode xn1 in xnl3) // reiterate the subset
{
Xmlelement xe2 = (xmlelement) xn1; // Conversion Type
If (xe2.name = "email") // If the node name is email
{
Xe2.innertext = TBN. Text; // replace the value of this node with the value in the text box.
Break;
}
}
Break;
}
}
}
XD. Save (server. mappath ("\ XML \ doc. xml "));
Fill ();
}
}
}

XML file
<? XML version = "1.0" encoding = "UTF-8"?>
<Dbguest>
<User>
<Name> Day bombing </Name>
<City> Jiangyou </city>
<Email> 7665@fds.com </Email>
<Time> 16:10:56 </time>
</User>
<User>
<Name> Tian hongchuan </Name>
<City> Mianyang </city>
<Email> th@DFs.com </Email>
<Time> 16:06:51 </time>
</User>
<User>
<Name> Huang Xiaomei </Name>
<City> Nanchong </city>
<Email> 5543@ds.com </Email>
<Time> 16:07:15 </time>
</User>
</Dbguest>

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.