asp.net read, write, add, modify, delete operations on XML files __.net
Source: Internet
Author: User
asp.net read, write, add, modify, delete operations on XML files
The following code debugging is correct
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;
XmlElement el=xmldoc.createelement ("person"); Add Person node
El. SetAttribute ("Name", "Wind and Cloud"); Add the Person node's property ' name '
El. setattribute ("Sex", "female"); Add the property "sex" of the person node
El. SetAttribute ("Age", "25"); Add the property "age" of the person node
XmlElement xesub1=xmldoc.createelement ("Pass"); Add a node in the person node
Xesub1. innertext= "123";//Set Text node
El. AppendChild (XESUB1);
XmlElement xesub2=xmldoc.createelement ("address");
Xesub2. innertext= "Kunming";/Set Text node
El. AppendChild (XESUB2);
modifying nodes
private void Updateelement ()
{
Loadxml ();
XmlNodeList Nodelist=xmldoc.selectsinglenode ("user"). childnodes;//gets all the child nodes of the bookstore node
foreach (XmlNode xn in nodelist)//traverse all child nodes
{
XmlElement xe= (XmlElement) xn;//Converts a child node type to a XmlElement type
if (XE. GetAttribute ("name") = = "Cloud")//If the Name property value is "cloud"
{
Xe. SetAttribute ("name", "invention");
If there are child nodes down here
XmlNodeList Nls=xe. CHILDNODES;//continues to fetch all child nodes of the XE child node
foreach (XmlNode xn1 in NLS)//traversal
{
XmlElement xe2= (XmlElement) xn1;//conversion type
if (Xe2. name== "pass")//If found
{
Xe2. innertext= "66666";//Modify
Break
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.