Web. config Configuration:
CopyCode The Code is as follows: <etettings>
<Add key = "xmlfile" value = "XML/class. xml"/>
</Appsettings>
<Deleetask>
<Add key = "xmlfile" value = "XML/class. xml"/>
</Appsettings>
Front-end: Copy code The Code is as follows: <% @ page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "test_default" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> C # xml (add, delete, modify, and query) exercises </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div id = "showxml" runat = "server">
Show XML document
</Div>
<Div style = "background-color: green; color: yellow;" style = "background-color: green; color: yellow; "> two key points for binding server controls to HTML controls: <br/>
1. onserverclick = "servermethod" only write the method name here. <br/>
2. Background code, which must be <br/>
Protected void xmladd (Object sender, eventargs e) {}< br/>
Pay attention to the two parameters and the protection level.
</Div>
<Input id = "btnadd" type = "button" value = "add" runat = "server" onserverclick = "xmladd"/>
<Input id = "btndelete" type = "button" value = "delete" runat = "server" onserverclick = "xmldelete"/>
<Input id = "btnupdate" type = "button" value = "Update" runat = "server" onserverclick = "xmlupdate"/>
<Input id = "btnquery" type = "button" value = "query" runat = "server" onserverclick = "xmlquery"/>
</Form>
</Body>
</Html>
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "test_default" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> C # xml (add, delete, modify, and query) exercises </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div id = "showxml" runat = "server">
Show XML document
</Div>
<Div style = "background-color: green; color: yellow;" style = "background-color: green; color: yellow; "> two key points for binding server controls to HTML controls: <br/>
1. onserverclick = "servermethod" only write the method name here. <br/>
2. Background code, which must be <br/>
Protected void xmladd (Object sender, eventargs e) {}< br/>
Pay attention to the two parameters and the protection level.
</Div>
<Input id = "btnadd" type = "button" value = "add" runat = "server" onserverclick = "xmladd"/>
<Input id = "btndelete" type = "button" value = "delete" runat = "server" onserverclick = "xmldelete"/>
<Input id = "btnupdate" type = "button" value = "Update" runat = "server" onserverclick = "xmlupdate"/>
<Input id = "btnquery" type = "button" value = "query" runat = "server" onserverclick = "xmlquery"/>
</Form>
</Body>
</Html>
Background: Copy code The Code is as follows: using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. xml;
Public partial class test_default: system. Web. UI. Page
{
String xmlfile = system. configuration. configurationmanager. deleettings ["xmlfile"];
Xmldocument xmldoc = new xmldocument ();
Protected void page_load (Object sender, eventargs E)
{
BIND ();
}
Private void BIND ()
{
Xmldoc. Load (server. mappath ("../" + xmlfile ); //
This. showxml. innerhtml = system. Web. httputility. htmlencode (xmldoc. innerxml );
}
Protected void xmladd (Object sender, eventargs E)
{
Xmlnode objrootnode = xmldoc. selectsinglenode ("// root"); // declare the xmlnode object
Xmlelement objchildnode = xmldoc. createelement ("student"); // create an xmlelement object
Objchildnode. setattribute ("ID", "1 ");
Objrootnode. appendchild (objchildnode );
//
Xmlelement objelement = xmldoc. createelement ("name ");//??? What are the differences between nodes and elements? The methods are the same.
Objelement. innertext = "tree1 ";
Objchildnode. appendchild (objelement );
// Save
Xmldoc. Save (server. mappath ("../" + xmlfile ));
}
Protected void xmldelete (Object sender, eventargs E)
{
String node = "// root/student [name = 'tree1 ']"; // XML is case sensitive.
Xmldoc. selectsinglenode (node). parentnode. removechild (xmldoc. selectsinglenode (node ));
// Save
Xmldoc. Save (server. mappath ("../" + xmlfile ));
}
Protected void xmlupdate (Object sender, eventargs E)
{
// Xmldoc. selectsinglenode ("// root/student [name = 'tree1 ']/Name"). innertext = "tree2 ";
Xmldoc. selectsinglenode ("// root/student [name = 'tree1 ']"). attributes ["ID"]. value = "001 ";
// Save
Xmldoc. Save (server. mappath ("../" + xmlfile ));
}
Protected void xmlquery (Object sender, eventargs E)
{
Xmlnodelist nodelist = xmldoc. selectnodes ("// root/student"); // query all student nodes
// Cyclically traverse the node to check whether the node exists
For (INT I = 0; I <nodelist. Count; I ++)
{
Response. Write (nodelist [I]. childnodes [0]. innertext );
}
// Query a single node. // It indicates all matching elements./It indicates the child element of the root. The query in Javascript is the same.
String xmlpathnode = "// root/student [name = 'Rock ']/photo ";
Response. Write (xmldoc. selectsinglenode (xmlpathnode). innertext );
}
}
Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. xml;
Public partial class test_default: system. Web. UI. Page
{
String xmlfile = system. configuration. configurationmanager. deleettings ["xmlfile"];
Xmldocument xmldoc = new xmldocument ();
Protected void page_load (Object sender, eventargs E)
{
BIND ();
}
Private void BIND ()
{
Xmldoc. Load (server. mappath ("../" + xmlfile ); //
This. showxml. innerhtml = system. Web. httputility. htmlencode (xmldoc. innerxml );
}
Protected void xmladd (Object sender, eventargs E)
{
Xmlnode objrootnode = xmldoc. selectsinglenode ("// root"); // declare the xmlnode object
Xmlelement objchildnode = xmldoc. createelement ("student"); // create an xmlelement object
Objchildnode. setattribute ("ID", "1 ");
Objrootnode. appendchild (objchildnode );
//
Xmlelement objelement = xmldoc. createelement ("name ");//??? What are the differences between nodes and elements? The methods are the same.
Objelement. innertext = "tree1 ";
Objchildnode. appendchild (objelement );
// Save
Xmldoc. Save (server. mappath ("../" + xmlfile ));
}
Protected void xmldelete (Object sender, eventargs E)
{
String node = "// root/student [name = 'tree1 ']"; // XML is case sensitive.
Xmldoc. selectsinglenode (node). parentnode. removechild (xmldoc. selectsinglenode (node ));
// Save
Xmldoc. Save (server. mappath ("../" + xmlfile ));
}
Protected void xmlupdate (Object sender, eventargs E)
{
// Xmldoc. selectsinglenode ("// root/student [name = 'tree1 ']/Name"). innertext = "tree2 ";
Xmldoc. selectsinglenode ("// root/student [name = 'tree1 ']"). attributes ["ID"]. value = "001 ";
// Save
Xmldoc. Save (server. mappath ("../" + xmlfile ));
}
Protected void xmlquery (Object sender, eventargs E)
{
Xmlnodelist nodelist = xmldoc. selectnodes ("// root/student"); // query all student nodes
// Cyclically traverse the node to check whether the node exists
For (INT I = 0; I <nodelist. Count; I ++)
{
Response. Write (nodelist [I]. childnodes [0]. innertext );
}
// Query a single node. // It indicates all matching elements./It indicates the child element of the root. The query in Javascript is the same.
String xmlpathnode = "// root/student [name = 'Rock ']/photo ";
Response. Write (xmldoc. selectsinglenode (xmlpathnode). innertext );
}
}
XML file Copy code The Code is as follows: <? XML version = "1.0" encoding = "gb2312"?>
<Root>
<Student admin = "no">
<Name> Rock </Name>
<Nickname> rock1 </nickname>
<PWD> 123 </pwd>
<Sex> boys </sex>
<Birthday> 1986-1-1 </birthday>
<Email> xymac@163.com </Email>
<QQ> 123374355 </QQ>
<MSN> loveplc@live.cn </MSN>
<Tel> 13005129336 </Tel>
<Homepage> http://www.jb51.net <Address> Guangzhou </address>
<Work> Asp.net cainiao </work>
<Photo> images/rock.gif </photo>
<Time> 10:15:29 </time>
</Student>
<Student admin = "yes">
<Name> tree </Name>
<Nickname> dormitory boss </nickname>
<PWD> 51 aspx </pwd>
<Sex> boys </sex>
<Birthday>
</Birthday>
<Email> support@tree.com </Email>
<QQ>
</QQ>
<MSN>
</MSN>
<Tel>
</Tel>
<Homepage>
</Homepage>
<Address>
</Address>
<Work>
</Work>
<Photo>
</Photo>
<Time> 11:39:57 </time>
</Student>
<Student>
<Name> tree2 </Name>
</Student>
<Student ID = "001">
<Name> tree1 </Name>
</Student>
</Root>