Asp.net (C #) Xml operation (add, delete, modify, and query) Exercise

Source: Internet
Author: User

Web. config Configuration:
Copy codeThe Code is as follows:
<Deleetask>
<Add key = "xmlFile" value = "xml/class. xml"/>
</AppSettings>
<Deleetask>
<Add key = "xmlFile" value = "xml/class. xml"/>
</AppSettings>

Front-end:
Copy codeThe 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 codeThe 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 codeThe 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 </Homepage>
<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>

Related Article

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.