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