. Net series: C # operating XML: Reading and Writing XML documents through xmldocument

Source: Internet
Author: User
Tags cdata xpath
. Net series: C # operating XML What is XML?
XML is short for the Extended Markup Language and a developed text format. For more information, see the W3 organization for http://www.w3.org/tr/1998/rec-xml-19980210. If you don't know about it, you will be out too much. How does. Net Process XML?
1. read and Write XML documents through xmldocument 2. use xmlreader to read XML, use xmlwriter to write xml3. use LINQ to XML to access xml4. use xmlscheme to define fixed-format XML documents. 5. XML serialization or deserialization Class 6. search for XML nodes using XPath 7. convert XML format using XSLT Read and Write XML documents through xmldocument There is an XML section as follows:
<? XML version = "1.0" encoding = "UTF-8"?> <Students> <! -- I am a text note --> <Student name = "Zhang Ping"> <courses> <Course name = "? "> <Teachercomment> <! [CDATA [here is the Chinese instructor's comment]> </teachercomment> </course> <Course name = "Mathematics"> <teachercomment> <! [CDATA [here is a math's comment]> </teachercomment> </course> </courses> </student> </students>
1. How to Use xmldocument to read XML
I want to use a paragraph Code Traverse all student and print all attributes and values of student
/* Yukai blog http://www.cnblogs.com/yukaizhao/ */using system; using system. collections. generic; using system. text; using system. XML; namespace xmlexample {class program {static void main (string [] ARGs) {string xmlfilepath = @ "X: \ about.net \ example \ xmlexample \ 1.xml "; xmldocument Doc = new xmldocument (); Doc. load (xmlfilepath); // use the XPath expression to select all the student subnodes in the document xmlnodelist studentnodelist = Doc. selectnodes ("/STU Dents/student "); If (studentnodelist! = NULL) {foreach (xmlnode studentnode in studentnodelist) {// get the attribute string name = studentnode whose attribute name is name through attributes. attributes ["name"]. value; console. writeline ("Student:" + name); // use the selectsinglenode method to obtain the courses subnode xmlnode coursesnode = studentnode under the current node. selectsinglenode ("courses"); // obtain all the first-level subnodes of coursenode using the childnodes attribute xmlnodelist coursenodelist = coursesnode. childnodes; If (coursenodelist! = NULL) {foreach (xmlnode coursenode in coursenodelist) {console. write ("\ t"); console. write (coursenode. attributes ["name"]. value); console. write ("Instructor's comment"); // you can get the first subnode of the course node through the firstnode attribute, and lastnode can get the last subnode xmlnode teachercommentnode = coursenode. firstchild; // read CDATA node xmlcdatasection CDATA = (xmlcdatasection) teachercommentnode. firstchild; console. writeline (CDATA. innertext. trim () ;}}} console. write ("\ r \ npress any key to continue .... "); console. read ();}}}
Xmldocument itself is inherited from xmlnode. To read XML nodes, you can use firstchild, lastchild, nextsibling, and previussibling to read a single node, or read all child nodes through childnodes. You can also use the XPath expression selectnodes (string XPath) or selectsinglenode (string XPath) to read one or more qualified nodes. 2. How to edit XML through xmldocument This is also an example of reading XML in XML. We will use the CSHARP code to generate xml, as shown in the following code:
/* Yukai blog http://www.cnblogs.com/yukaizhao/ */using system; using system. collections. generic; using system. text; using system. XML; namespace writexml {class program {static void main (string [] ARGs) {xmldocument xmldoc = new xmldocument (); // create an XML declaration section, that is, <? XML version = "1.0" encoding = "UTF-8"?> Xmldoc. createxmldeclaration ("1.0", "UTF-8", "yes"); // create the root node xmlnode rootnode = xmldoc. createelement ("Students"); // create the student subnode xmlnode studentnode = xmldoc. createelement ("student"); // create an attribute xmlattribute nameattribute = xmldoc. createattribute ("name"); nameattribute. value = "Zhang"; // attributes of the XML node attachment studentnode. attributes. append (nameattribute); // create the courses subnode xmlnode coursesnode = xmldoc. createelement ("courses"); xmlnode coursenode1 = xmldoc. createelement ("Course"); xmlattribute coursenameattr = xmldoc. createattribute ("name"); coursenameattr. value = ""; coursenode1.attributes. append (coursenameattr); xmlnode teachercommentnode = xmldoc. createelement ("teachercomment"); // create a CDATA block xmlcdatasection CDATA = xmldoc. createcdatasection ("<font color = \" Red \ "> This is a Chinese instructor's note </font>"); teachercommentnode. appendchild (CDATA); coursenode1.appendchild (teachercommentnode); coursesnode. appendchild (coursenode1); // append the subnode studentnode. appendchild (coursesnode); rootnode. appendchild (studentnode); // append the root node xmldoc. appendchild (rootnode); // Save the XML file xmldoc. save (@ "D: \ test. XML "); console. writeline ("saved XML document ");}}}

the main point of generating XML using xmldocument is to use createelement of the xmldocument instance to create an xmlnode or use the createattribute method to create an attribute and appendchild to attach an XML node, appendattribute is used to attach attribute to the attribute set of a node.

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.