The XML file is written, and the collection XML is serialized (written). Read, recursive traversal of XML files

Source: Internet
Author: User

XML file: You must have a node. Examine the XML file, which can be opened with a browser, open to indicate, or wrong.

Processing method:

Dom:xmldocument Document Object Model

Sax (Event-driven, XmlReader)

XmlSerializer (serialization): A class, a generic collection, can be serialized.

Linq to XML: Mainly used in classes: Xelement XDocument xattribute using System.Xml.Linq;

write an XML file:

School, class, student (gender, age, age)

1 namespaceW_r_xml_model2 {3     class Program4     {5         Static voidMain (string[] args)6{//Create a document7XDocument xdoc=NewXDocument ();8             //This document must contain a root node with the name of the node named (), not the variable name. 9XElement xx=NewXElement ("Xuexiao");Ten Xdoc. Add (xx); One             //Create a node AXElement xbanji=NewXElement ("Banji"); -XElement xxuesheng=NewXElement ("Xuesheng"); -XElement xxingming=NewXElement ("xingming"); theXElement xxingbie=NewXElement ("Xingbie"); -XElement xgender=NewXElement ("Gender"); -             //Depending on which node is under which node, the node is added. Nodes of the same level can be added as an array to the parent node add, adding the attribute is also add, but first Newxattribute - Xxuesheng. ADD (Xxingming,xxingbie,xgender); + Xbanji. ADD (Xxuesheng); - xx. ADD (Xbanji); +             //assigns a value to a node. value A             //Xxuesheng. Value = "Rico"; atXxingming. Value ="Rico"; -Xxingbie. Value ="male"; -Xgender. Value =" at"; -             //to add a property to a node, first create the property XAttribute -XAttribute xuexiaoattribute=NewXAttribute ("ID",00001); - xx. ADD (Xuexiaoattribute); inXAttribute banjiattribute=NewXAttribute ("ID", Geneva); - Xbanji. ADD (Banjiattribute); toXAttribute xueshengattribute=NewXAttribute ("ID",024); + Xxuesheng. ADD (Xueshengattribute); -  the             //Save File *Xdoc. Save ("Lizi.xml"); $         }Panax Notoginseng     } -}

serialization (writes a generic collection to an XML file): Often the method of exporting a database as an XML document can be set up first. But in this way, all data is added to the list memory at once, with XmlReader, and XmlWriter when the data is large.

1 namespaceW_r_xml_model2 {3     //Mark First, then public4 [Serializable]5      Public classOneperson6     {7          Public stringName {Get;Set; }8          Public intAge {Get;Set; }9          Public CharGender {Get;Set; }Ten     } One     class Program A     { -         Static voidMain (string[] args) -         { thelist<oneperson> list =Newlist<oneperson>//collection initializers can be omitted () -             { -                 NewOneperson {Name ="Rico", age = -, Gender ='male'},//object initializers can be omitted () -                 NewOneperson {Name ="Xiao Cui", age = -, Gender ='female'}, +                 NewOneperson {Name ="Little Red", age = -, Gender ='female'} -             }; +             //serializes this generic collection into XML-formatted files A             using(FileStream file =NewFileStream ("Lisxm.xml", FileMode.Create, FileAccess.Write)) at             { -                 //find someone to serialize the type of the parameter serialization, -XmlSerializer serializer =NewXmlSerializer (typeof(list<oneperson>)); -                 //serialized objects and destination file streams - Serializer. Serialize (file, list); -             } in         } -     } to}

Unicode code 30007,22899 for men and women

read the XML document;

Get the document. Xdocment.load (PATH)

Gets all the child nodes under the root node. Xelement.root

Loops a child node, obtaining a child node under a child node.

The XML file is written, and the collection XML is serialized (written). Read, recursive traversal of XML files

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.