<?xml version= "1.0" encoding= "gb2312" standalone= "yes"? ><! DOCTYPE person SYSTEM "PERSON.DTD" ><?xml-stylesheet href= "hello.css" type = "Text/css"?><persons> <person description= "The Person of the Dragon and Phoenix posture of the daylight table;" > <name><![ Cdata[
It's easy to use.
Third, the output of XML dataXElement has a save, this save is very powerful and has multiple overloads that support the input of XML data everywhere (file address, stream, textwriter,xmlwriter).
Class Program {static void Main (string[] args) {XElement xml = new Xeleme NT ("Persons", New XElement ("person", New XElement ("Name", "Liu Bei"), New XElement ("Age", "28")); Output to the file XML. Save (@ "D:\123.xml"); Output to TextWriter TextWriter tw = new StringWriter (); The second parameter of the SaveOptions enumeration supports formatting, indenting, preserving extraneous important whitespace, and removing duplicate namespace XML. Save (TW, Saveoptions.none); Console.WriteLine (TW); Output to stream using (MemoryStream mem = new MemoryStream ()) {XML. Save (MEM); Console.WriteLine (Encoding.UTF8.GetString (MEM). ToArray ())); }//Output to XmlWriter XmlWriter XW = xmlwriter.create (@ "D:\LinqToXml.xml"); Xml. Save (XW); Xw. Flush (); Console.readkey (); } }
A demo to complete the output to 4 locations, good.
Iv. input of XML data 1. Input XML data from the file
The output of the XML data is powerful, and the input to the XML data is equally powerful, with multiple overloaded load support for importing XML data from Uri,textwriter,xmlreader.
static void Main (string[] args) { //Gets the XML data from the URI, supports local paths and URLs, supports settings for the corresponding enumeration XElement xe1 = xelement.load (@ "D:\ 123.xml ", loadoptions.none); Load XmlReader XR = xmlreader.create (@ "D:\123.xml") from XmlReader; XElement xe2 = xelement.load (XR); Get data from TextReader TextReader reader = File.OpenText (@ "D:\123.xml"); XElement xe3 = xelement.load (reader); Read from stream XElement xe4 = xelement.load (new FileStream (@ "D:\123.xml", FileMode.Open,FileAccess.Read)); Console.readkey (); }
2. Entering XML data from a string
Parsing the data from a string into XML requires only the Parse method.
static void Main (string[] args) { string xmlstring = ' <?xml version=\ ' 1.0\ ' encoding=\ ' utf-8\ '?>< Persons><person><name> Liu Bei </Name><Age>28</Age></Person></Persons> "; XElement XE = Xelement.parse (xmlstring,loadoptions.setlineinfo); Console.readkey (); }
Simple and easy to use
V. Basic XML QueryBasic queries are much simpler, similar to jquery selectors, and chained.
static void Main (string[] args) { string xmlstring = ' <?xml version=\ ' 1.0\ ' encoding=\ ' utf-8\ '?>< Persons><person description=\ "Niu Man" ><Name> Liu Bei </Name><Age>28</Age></Person> </Persons> "; XElement XE = Xelement.parse (xmlstring,loadoptions.setlineinfo); Read property value string arrivalue = Xe. Elements ("person"). First (). Attribute ("Description"). Value; or //string arrivalue = Xe. Element ("person"). Attribute ("Description"). Value; Console.WriteLine (arrivalue); Output OX //Read element read the first sibling element behind name XElement xe1 = Xe. Element ("person"). Element ("Name"). Elementsafterself (). First (); Console.WriteLine (Xe1. Value); Output //Read parent node XElement xe2 = xe1. Parent; Console.WriteLine (Xe2. Name); Console.readkey (); }
Other queries:
static void Main (string[] args) {string xmlstring = "<?xml version=\" 1.0\ "encoding=\" utf-8\ "?" ><persons><person description=\ "niu man \" ><Name> Liu Bei </name><age>28</age></ Person><person><name> Guan Yu </Name><Age>26</Age></Person></Persons> "; XElement XE = Xelement.parse (xmlstring, loadoptions.setlineinfo); Query the person node that contains the attribute list<xelement> listelement = Xe. Descendants ("Person"). Where (x = x.hasattributes). ToList (); foreach (XElement x in Listelement) {Console.WriteLine (x.value); Output Liu Bei 28}//Query person with age 26 person node list<xelement> ListElement2 = Xe. Descendants ("Person"). Where (x = x.element ("Age"). Value = = "26"). ToList (); foreach (XElement x in ListElement2) {Console.WriteLine (x.value); Output Guan Yu, CoNsole. ReadKey (); }
Nodes, descendants, and their afterself are used for LINQ queries that turn into ienumerable<t>.
Vi. Modifying XMLModifying XML is also very simple, no more nonsense. Give a simple example directly:
static void Main (string[] args) { string xmlstring = ' <?xml version=\ ' 1.0\ ' encoding=\ ' utf-8\ '?>< Persons><person description=\ "Niu Man" ><Name> Liu Bei </Name><Age>28</Age></Person> </Persons> "; XElement XE = Xelement.parse (xmlstring, loadoptions.setlineinfo); This is added at the end, if you want to add at the front can use AddFirst, //Add addbeforself before this node, add addafterself Xe after this node . ADD (New XElement ("person", new XElement ("Name", "Guan Yu"), New XElement ("Age", "page")) ; Remove all child node XE. RemoveAll (); TextWriter tw = new StringWriter (); The second parameter of the SaveOptions enumeration supports formatting, indenting, preserving extraneous important whitespace, and removing the duplicate namespace XE. Save (TW, saveoptions.none); Console.WriteLine (TW); Console.readkey (); }
LINQ Learning Notes---LINQ to XML operations