1 Code
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 usingSystem.Xml.Linq;7 8 namespaceConsoleApplication89 {Ten One class Program A { - Static voidMain (string[] args) - { theXDocument xmlfile =NewXDocument (); - - //setting the root element -XElement root =NewXElement ("Cultures"); + - //Establish a node node; <daojia count= "comment=" good "> +XElement Daojia =NewXElement ("Daojia"); ADaojia. Setattributevalue ("Count"," -"); atDaojia. Setattributevalue ("Comment","Good"); - - //to make such an effect - //<book> - //<name> ethics through </name> - //<author> Lao Zi </author> in //</book> - toXElement Book1 =NewXElement (" Book"); +Book1. Setelementvalue ("name","Moral Sutra"); -Book1. Setelementvalue ("author","Lao Tzu");//Lee Ear, is rumored to be Lao Zi, hey. the //No matter who he is, anyway, the moral of the book is super intelligent. * $XElement Book2 =NewXElement (" Book");Panax NotoginsengBook2. Setelementvalue ("name","The beginning of the literary scriptures"); -Book2. Setelementvalue ("author","Guan Yin");//that's where he's Hanguguan . the + Daojia. ADD (BOOK1); A Daojia. ADD (BOOK2); the //The first-level child node of the Daojia programming cultures + Root. ADD (Daojia); - $ $ //Establish a node node; <fojia count= "321" comment= "good" > -XElement Fojia =NewXElement ("Fojia"); -Fojia. Setattributevalue ("Count","321"); theFojia. Setattributevalue ("Comment","Good"); - Wuyi //<book > the //<name> Diamond Sutra </name> - //<author> Sakyamuni Buddha </author> Wu //</book> - //<book> About //<name> Tibetan Bodhisattva Sutra </name> $ //<author> Buddha in Tibet </author> - //</book> - -XElement BOOK3 =NewXElement (" Book"); ABook3. Setelementvalue ("name","Diamond Sutra");//I can't read the book. Who has great wisdom, can challenge +Book3. Setelementvalue ("author","Sakyamuni Buddha"); the -XElement BOOK4 =NewXElement (" Book"); $Book4. Setelementvalue ("name","The sutra of the Buddha in Tibet");//The story of my daughter's saving mother theBook4. Setelementvalue ("author","Buddha of the King of Tibet"); the the Fojia. ADD (BOOK3); the Fojia. ADD (BOOK4); - in //The first-level child node of the Fojia programming cultures the Root. ADD (Fojia); the About the //The root node can only be one the Xmlfile.add (root); the //Save File +Xmlfile.save ("Test.xml"); - theConsole.WriteLine ("OK");Bayi Console.readkey (); the } the } -}
2 effects
-Console
-Documents
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Cultures>3 <DaojiaCount= "+"Comment= "good">4 < Book>5 <name>Moral sutra</name>6 <author>Laozi</author>7 </ Book>8 < Book>9 <name>The beginning of the literary scriptures</name>Ten <author>Guan Yin</author> One </ Book> A </Daojia> - <FojiaCount= "321"Comment= "good"> - < Book> the <name>Diamond Sutra</name> - <author>Shakyamuni Buddha</author> - </ Book> - < Book> + <name>The sutra of the Buddha in Tibet</name> - <author>Buddha of the King of Tibet</author> + </ Book> A </Fojia> at </Cultures>
C # XML writes out an XML file with the console