Process read/write XML Summary In. Net C #

Source: Internet
Author: User

Using system. xml;

 

Initialize an XML instance

Xmldocument xml = new xmldocument ();

Import a specified XML file

XML. Load (PATH );

XML. Load (httpcontext. Current. server. mappath ("../AA. xml "));

Specify a node

Xmlnode root = xml. selectsinglenode ("/root ");

Obtain all direct subnodes under a node

Xmlnodelist childlist = root. childnodes;

Determines whether a subnode exists under the node.

Root. haschildnodes;

Obtains a set of same-level nodes with the same name.

Xmlnodelist nodelist = xml. selectnodes ("/root/News ");

Generate a new node

Xmlelement node = xml. createelement ("news ");

Add a node to a specified node as its subnode

Root. appendchild (node );

Add a node to a subnode under a specified Node

Root. insertbefore (node, root. childenodes [I]);

Creates a new attribute for a specified node and assigns a value to it.

Node. setattribute ("ID", "11111 ");

Add a subnode to a specified Node

Root. appendchild (node );

Obtains the attribute value of a specified node.

String id = node. attributes ["ID"]. value;

Obtain text from a specified Node

String content = node. innertext;

Save the XML file

XML. Save (PATH );

XML. Save (httpcontext. Current. server. mappath ("../AA. 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.