ASP. Read/write to XML file, add, modify, delete operations

Source: Internet
Author: User

usingSystem;usingSystem.Collections;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingsystem.web;usingSystem.Web.SessionState;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.HtmlControls;usingSystem.Xml;PrivateXmlDocument xmldoc; //Load XML file        Private voidLoadXml () {xmldoc=NewXmlDocument (); Xmldoc.load (Server.MapPath ("User.xml")); }         //Adding nodes        Private voidaddelement () {LoadXml (); XmlNode Xmldocselect=xmldoc.selectsinglenode ("User"); XmlElement El=xmldoc.createelement (" Person");//Add Person nodeEl. SetAttribute ("name","Cloud");//Add the property "name" of the person nodeEl. SetAttribute ("Sex","female");//Add the properties of the person node "sex"El. SetAttribute (" Age"," -");//Add the property "age" of the person nodeXmlElement xesub1=xmldoc.createelement ("Pass");//Add a node in the person nodeXesub1. innertext="123";//Set the text nodeel.             AppendChild (XESUB1); XmlElement XESUB2=xmldoc.createelement ("Address"); Xesub2. InnerText="Kunming";//Set the text nodeel.             AppendChild (XESUB2);            Xmldocselect.appendchild (EL); Xmldoc.save (Server.MapPath ("User.xml")); }         //Modify Node        Private voidupdateelement () {LoadXml (); XmlNodeList NodeList=xmldoc.selectsinglenode ("User"). ChildNodes;//get all child nodes of the bookstore node            foreach(XmlNode xninchNodeList)//Traverse all child nodes{XmlElement XE= (XmlElement) xn;//To Convert a child node type to a XmlElement type                if(XE. GetAttribute ("name")=="Cloud")//if the Name property value is "cloud"{XE. SetAttribute ("name","Inventions"); //If there are child nodes below, go down.XmlNodeList Nls=xe. ChildNodes;//continue to acquire all child nodes of the XE child node                    foreach(XmlNode xn1inchNls//Traverse{XmlElement Xe2= (XmlElement) xn1;//Conversion Type                        if(Xe2. name=="Pass")//If you find{Xe2. InnerText="66666";//then modify                             Break; }                    }                      Break; }} xmldoc.save (Server.MapPath ("User.xml"));//Save        }          //Delete a node        Private voidDeletenode () {LoadXml (); XmlNodeList XNL=xmldoc.selectsinglenode ("User").              ChildNodes; foreach(XmlNode xninchxnl) {XmlElement XE=(XmlElement) xn; if(XE. GetAttribute ("name")=="Inventions")                {                    //XE. RemoveAttribute ("name");//Remove the Name propertyXe. RemoveAll ();//Delete the entire contents of the node                     Break; }} xmldoc.save (Server.MapPath ("User.xml"));//Save        }         Private voidShowit () {LoadXml (); XmlNode xn=xmldoc.selectsinglenode ("User"); XmlNodeList XNL=xn.                ChildNodes; foreach(XmlNode xnfinchxnl) {XmlElement XE=(XmlElement) xnf;//Console.WriteLine (XE. GetAttribute ("name"));//Display property Values//Console.WriteLine (XE. GetAttribute ("Sex"));// //XmlNodeList Xnf1=xe. ChildNodes;//foreach (XmlNode xn2 in Xnf1)//                {//Console.WriteLine (xn2. InnerText);//Show child node point text//                }}} XML style:<?xml version="1.0"encoding="gb2312"?><user> <person> </person> <person name="Wind Pull"sex="male"Age=" -"> <pass>123</pass> <Address> daming </Address> </person> <person name="Cloud"sex="female"Age=" -"> <pass>123</pass> <Address> Kunming </Address> </person></user>

ASP. Read/write to XML file, add, modify, delete operations

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.