Example of adding, deleting, and modifying xml operations in asp.net

Source: Internet
Author: User

Copy codeThe Code is as follows:
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. Xml;
Private XmlDocument xmlDoc;
// Load xml file
Private void LoadXml ()
{
XmlDoc = new XmlDocument ();
XmlDoc. Load (Server. MapPath ("User. xml "));
}
// Add a node
Private void AddElement ()
{
LoadXml ();
XmlNode xmldocSelect = xmlDoc. SelectSingleNode ("user ");
XmlElement el = xmlDoc. CreateElement ("person"); // Add a person Node
El. SetAttribute ("name", ""); // Add the property of the person node "name"
El. SetAttribute ("sex", "female"); // Add the property of the person node "sex"
El. SetAttribute ("age", "25"); // Add the property of the person node "age"
XmlElement xesub1 = xmlDoc. CreateElement ("pass"); // Add a node in the person Node
Xesub1.InnerText = "123"; // set a text node
El. AppendChild (xesub1 );
XmlElement xesub2 = xmlDoc. CreateElement ("Address ");
Xesub2.InnerText = "Kunming"; // set a text node
El. AppendChild (xesub2 );
XmldocSelect. AppendChild (el );
XmlDoc. Save (Server. MapPath ("user. xml "));
}
// Modify a node
Private void UpdateElement ()
{
LoadXml ();
XmlNodeList nodeList = xmlDoc. SelectSingleNode ("user"). ChildNodes; // obtain all the subnodes of the bookstore Node
Foreach (XmlNode xn in nodeList) // traverses all subnodes
{
XmlElement xe = (XmlElement) xn; // converts the subnode type to the XmlElement type
If (xe. GetAttribute ("name") = "") // if the attribute value of name is ""
{
Xe. SetAttribute ("name", "invention"); // if there are subnodes below
XmlNodeList nls = xe. ChildNodes; // continue to obtain all the child nodes of the xe subnode
Foreach (XmlNode xn1 in nls) // traverse
{
XmlElement xe2 = (XmlElement) xn1; // Conversion Type
If (xe2.Name = "pass") // if you find
{
Xe2.InnerText = "66666"; // modify
Break;
}
}
Break;
}
}
XmlDoc. Save (Server. MapPath ("user. xml"); // Save
}
// Delete a node
Private void deleteNode ()
{
LoadXml ();
XmlNodeList xnl = xmlDoc. SelectSingleNode ("user"). ChildNodes;
Foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement) xn;
If (xe. GetAttribute ("name") = "invention ")
{
// Xe. RemoveAttribute ("name"); // delete the name attribute
Xe. RemoveAll (); // delete all content of the node
Break;
}
}
XmlDoc. Save (Server. MapPath ("user. xml"); // Save
}
Private void showIt ()
{
LoadXml ();
XmlNode xn = xmlDoc. SelectSingleNode ("user ");
XmlNodeList xnl = xn. ChildNodes;
Foreach (XmlNode xnf in xnl)
{
XmlElement xe = (XmlElement) xnf;
// Console. WriteLine (xe. GetAttribute ("name"); // display the attribute value
// Console. WriteLine (xe. GetAttribute ("sex "));
/// XmlNodeList xnf1 = xe. ChildNodes;
// Foreach (XmlNode xn2 in xnf1 )//
{
// Console. WriteLine (xn2.InnerText );
// Display the child node text
//}
}
}

Xml style:
Copy codeThe Code is as follows:
<? Xml version = "1.0" encoding = "gb2312"?>
<User>
<Person> </person>
<Person name = "fengla" sex = "male" age = "25">
<Passed> 123 </pass>
<Address> Daming </Address>
</Person>
<Person name = "" sex = "" age = "25">
<Passed> 123 </pass>
<Address> Kunming </Address>
</Person>
</User>

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.