C # Summary of basic XML operations

Source: Internet
Author: User

C # basic XML operations include reading node data and adding nodes. Read node attributes and modify node attributes. The details are as follows:

XML file: The file is in the mydocument folder.

 
<? XML version = "1.0" encoding = "UTF-8"?> <Personf xmlns = "" name = "(TEST) Work Hard Work smart! "> <Person name =" person1 "> <ID> 1 </ID> <Name> xiaoa </Name> <age> 59 </age> </person> <person name = "person2"> <ID> 2 </ID> <Name> xiaob </Name> <age> 29 </age> </person> <person name = "person3 "> <ID> 3 </ID> <Name> xiaoc </Name> <age> 103 </age> </person> <person name =" person4 "> <ID> 4 </ID> <Name> xiaod </Name> <age> 59 </age> </person> </personf>

Code: The description is in the comments.

Public void testxml () {xmldocument Doc = new xmldocument (); string Path = "http://www.cnblogs.com/MyDocument/Person.xml"; try {// Doc. load (server. mappath () Doc. load (PATH); // 1. Read the data of a single node xmlnode node = Doc. selectsinglenode ("personf"); // 2. read data from multiple nodes xmlnodelist nodelist1 = Doc. selectnodes ("personf/person"); // 3.1 read the specific value of a specific node, for example, innertext xmlnodelist nodelist = Doc. documentelement. Getelementsbytagname ("person"); foreach (xmlnode node2 in nodelist1) // Of course, the value of nodelist can also be used {If (node2.attributes ["name"]. innertext = "person2") {console. writeline (node2.childnodes [1]. innertext) ;}}// 3.2 read the innertext xmlnode node3 = Doc. selectsinglenode ("personf/person [ID = 2]"); string strnode3 = node3.childnodes [1]. innertext; // 3.3 Use the following method to find the node xmlnodelist nodelist2 = doc with ID 2. s Electnodes ("// person // ID"); xmlnode node4 = NULL; foreach (xmlnode node5 in nodelist2) {If (node5.innertext = "2") {node4 = node5; break;} console. writeline (node4.innertext); // 4. Read the node attribute string name = node. attributes ["name"]. innertext; // 5 modify node attributes. attributes ["name"]. innertext = "Work hard work smart! "; Doc. save (PATH); // 6 Add the custom node xmltextreader reader = new xmltextreader (PATH); xmlelement root = Doc. documentelement; // get the root node xmlelement tagouter = Doc. createelement ("person"); // sets the node attribute tagouter. setattribute ("name", "person5"); xmlelement tagin_name = Doc. createelement ("name"); xmlelement tagin_id = Doc. createelement ("ID"); tagin_name.innertext = "Work hard work smart! "; Tagin_id.innertext =" 32 "; tagouter. appendchild (tagin_name); tagouter. appendchild (tagin_id); root. appendchild (tagouter); // Add tagouter to the last reader of the XML file. close (); Doc. save (PATH);} catch (system. exception e) {Throw new exception (E. message );}}

The following describes the specific operations on XML in C #.

How to read and write XML documents in. net

C # operating XML selectnodes, selectsinglenode always returns NULL and XPath Introduction

C # Use selectsinglenode to parse XML files with multiple namespaces

 

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.