C # Read and write XML files

Source: Internet
Author: User

About XML is a relatively important thing, in the normal development process, the main thing is to master the content of XML read and write operations.

I. What is XML?

      • XML refers to Extensible Markup Language (extensible Markup Language)
      • XML is a markup language, much like HTML
      • XML is designed to transmit data rather than display it
      • XML tags are not predefined, you need to define your own labels
      • XML is designed to be self-descriptive
      • XML is the recommended standard for the consortium

Two. XML syntax:

1. An XML consists of the following parts:

Document declarations, elements, attributes, annotations, CDATA (special characters), processing instructions

2. The simplest form of declaration <?xml version= "1.0"?> use encoding attribute to declare the encoding of the document <?xml version= "1.0" encoding= "UTF-8"?> with Standa The lone property describes whether the document is independent <?xml version= "1.0" encoding= "UTF-8" standalone= "yes"?> three. Reading of the XML file: XML file contents:
<?XML version= "1.0" encoding= "Utf-8"?><LibraryID= "+">  < BookID= " the">    <name>Advanced mathematics</name>    <name1>College English</name1>  </ Book></Library>

Read XML content:

 Static voidMain (string[] args) {     //load the XML file inXDocument document = Xdocument.load ("D:\\123.xml"); //gets the root element of the XML to operate onXElement root=document.     Root; XElement Ele= root. Element (" Book"); //get the value of the name tagXElement shuxing= ele. Element ("name"); Console.WriteLine (shuxing.     Value); //gets all child elements under the root elementIenumerable<xelement> enumerable =Root.     Elements (); foreach(XElement IteminchEnumerable) {         foreach(XElement item1inchitem. Elements ()) {Console.WriteLine (item1.   Name); //Output name name1} Console.WriteLine (item. Attribute ("ID"). Value);//Output} console.readkey (); }

Write XML content:

 Static voidMain (string[] args) {     //get root Node objectXDocument document =NewXDocument (); XElement Root=NewXElement ("School"); XElement Book=NewXElement (" Book"); Book. Setelementvalue ("name","Advanced Mathematics"); Book. Setelementvalue ("name1","College English"); Root.     ADD (book); Root. Save ("D:\\123.xml"); Console.readkey ();}

Note: Here are two simple read write XML file, more advanced usage can refer to: Youdao Cloud Note XML part of the content http://note.youdao.com/share/?id=11080af8b23dbc3b89872f2d07d61 42e&type=note

C # Read and write XML files

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.