C # Basic Operations summary FOR XML

Source: Internet
Author: User

C # Basic operations for XML include reading data from nodes, adding nodes. Read node properties, modify node properties, and so on. Specific as follows:

XML file: File under 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 instructions are all 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 XmlNode node = doc for individual nodes.                selectSingleNode ("personf"); 2. Read data from multiple nodes XmlNodeList NodeList1 = doc.                SelectNodes ("Personf/person"); 3.1 Read specific values for specific nodes such as: The second node of the Person2 property name innertext XmlNodeList nodeList = doc.                Documentelement.getelementsbytagname ("person"); foreach (XmlNode node2 in NodeList1)//Can of course also use the value of NodeList {if (node2. attributes["Name"]. InnerText = = "Person2") {Console.WriteLine (node2. CHILDNODES[1].                    InnerText); }}//3.2 reads the node with ID 2 The second child node of name 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 with ID 2 xmlnodelist nodeList2 = doc.                SelectNodes ("//person//id");                XmlNode node4 = null; foreach (XmlNode node5 in NodeList2) {if (node5.                        InnerText = = "2") {node4 = NODE5;                    Break }} Console.WriteLine (Node4.                InnerText); 4. Read the properties of the node, string Name = node. attributes["Name"].                InnerText; 5 Modify the node's properties. attributes["Name"].                InnerText = "Work hard work smart!"; Doc.                Save (path);                6 Adding a custom node XmlTextReader reader = new XmlTextReader (path); XmlElement root = Doc. documentelement;//Gets the root node XmlElement tagouter = Doc.                createelement ("person"); //Set node Properties 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 is a concrete operation of the XML for C #

How to complete. NET read and write operations of XML documents

C # Operations XML Selectnodes,selectsinglenode always returns null with XPath introduction

parsing XML files with multiple namespaces using the selectSingleNode method in C #

  



Original link: http://www.cnblogs.com/linlf03/archive/2011/10/10/2205896.html

C # Basic Operations summary FOR 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.