Simple methods and instructions for. NET operations XML _ Practical Tips

Source: Internet
Author: User
Tags httpcontext
Copy Code code as follows:

Using System.Xml;
Initializing an instance of XML
XmlDocument xml=new XmlDocument ();
Import the specified XML file
Xml. Load (path);
Xml. Load (HttpContext.Current.Server.MapPath ("~/file/bookstore.xml"));
Specify a node
XmlNode Root=xml. selectSingleNode ("/root");
Get all direct child nodes under node
XmlNodeList Childlist=root. ChildNodes;
Determine if there are child nodes under this node
Root. HasChildNodes;
Gets a collection of sibling nodes with the same name
XmlNodeList Nodelist=xml. SelectNodes ("/root/news");
Generate a new node
XmlElement Node=xml. CreateElement ("News");
Adds a node under the specified node as its child node
Root. AppendChild (node);
Add a node before a child node under the specified node
Root. InsertBefore (Node,root. Childenodes[i]);
Creates a new property for the specified node and assigns a value
Node. SetAttribute ("id", "11111");
To add a child node to a specified node
Root. AppendChild (node);
Gets the specified property value for the specified node
String Id=node. attributes["id"]. Value;
Gets the text in the specified node
String Content=node. InnerText;
Save XML file
String Path=server.mappath ("~/file/bookstore.xml");
Xml. Save (path);
or Use:xml. Save (HttpContext.Current.Server.MapPath ("~/file/bookstore.xml"));

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.