Members of the LINQ to XML,
Property list:
Property |
Description |
Document |
Get XDocument of this XObject |
Emptysequence |
Gets the empty element collection |
FirstAttribute |
Gets the first property of this element |
Firstnode |
Gets the first child node of this node |
HasAttributes |
Gets a value that indicates whether this element has at least one property |
Haselements |
Gets a value that indicates whether this element has at least one child element |
IsEmpty |
Gets a value that indicates whether this element contains no content |
LastAttribute |
Gets the last property of this element |
Lastnode |
Gets the last child node of this node |
Name |
Gets or sets the name of this element |
NextNode |
Gets the next sibling node of this node |
NodeType |
Gets the node type for this node |
Parent |
Gets the parent of this XObject XElement |
Previousnode |
Gets the previous sibling node of this node |
Value |
Gets or sets the concatenated text content of this element |
List of methods:
Method |
Description |
Add |
Adds the specified content to the child of this XContainer |
Addafterself |
Add the specified content immediately after this node |
Addannotation |
Add an object to the annotation list for this XObject |
Addbeforeself |
Add the specified content immediately before this node |
AddFirst |
Adds the specified content as the first child of this document or element |
Ancestors |
Returns a collection of ancestor elements for this node |
Ancestorsandself |
Returns a collection of elements containing this element and its ancestor |
Annotation |
Gets the first annotation object of a specified type from this XObject |
Annotations |
Gets the annotation collection for the specified type of this XObject |
Attribute |
Returns the XAttribute of this XElement with the specified XName |
Attributes |
Returns the collection of attributes for this element |
Createreader |
Create XmlReader using the options specified by the readeroptions parameter |
Createwriter |
Create a XmlWriter that can be used to add nodes to XContainer |
Descendantnodes |
Returns a collection of descendant nodes for this document or element in document order |
Descendantnodesandself |
Returns a collection of nodes that contain this element and all descendant nodes of this element and arrange them in document order |
Descendants |
Returns a collection of descendant elements of this document or element in document order |
Descendantsandself |
Returns a collection of elements that contain this element and all descendant elements of this element, and arrange them in document order |
Elements |
Returns a filtered collection of child elements for this element or document in document order |
Elementsafterself |
Returns the collection of sibling elements after this node in document order |
Elementsbeforeself |
Returns the collection of sibling elements before this node in document order |
Getdefaultnamespace |
Gets the default XNamespace for this XElement |
Getnamespaceofprefix |
Gets the namespace associated with the specific prefix for this XElement |
Getprefixofnamespace |
Gets the prefix that is associated with this XElement namespace |
Isafter |
Determines whether the current node appears in document order after the specified node |
Isbefore |
Determines whether the current node appears in document order before the specified node |
Load |
Load XML data from file path, TextReader, XmlReader, stream |
Nodes |
Returns a collection of child nodes for this element or document in document order |
Nodesafterself |
Returns a collection of sibling nodes after this node in document order |
Nodesbeforeself |
Returns the collection of sibling nodes before this node in document order |
Parse |
Load XElement from a string containing XML |
Remove |
Remove this node from the node parent |
RemoveAll |
Remove nodes and attributes from this XElement |
Removeannotations |
Removes annotations of the specified type from this XObject |
Removeattributes |
Remove the properties of this XElement |
Removenodes |
Remove a child node from this document or element |
ReplaceAll |
Replaces the child nodes and attributes of this element with the specified content |
Replaceattributes |
Replaces the attribute of this element with the specified content |
Replacenodes |
Replaces the child node of this document or element with the specified content |
ReplaceWith |
Replace this node with the specified content |
Save |
Serializes this element to a file, XmlWriter, TextWriter, Stream |
Setattributevalue |
Set the value of a property, add a property, or remove an attribute |
Setelementvalue |
Set the value of a child element, add a child element, or remove a child element |
SetValue |
Set the value of this element |
WriteTo |
Writes this element to XmlWriter |
II. basic operations for various classes of LINQ to XML
1. Creating XML Elements
LINQ to XML uses the XElement class to create XML elements.
Let's take a look at one of the most basic examples:
Class program { static void Main (string[] args) { XElement xml = new XElement ("Persons", New XElement ("Person", new XElement ("Name", "Liu Bei"), New XElement ("Age", "page") ), new XElement ("person ", new XElement (" Name "," Guan Yu "), New XElement (" Age "," the ")) ; Xml. Save (@ "D:\123.xml"); Console.readkey (); } }
The above code generates the following XML file code:
<?xml version= "1.0" encoding= "Utf-8"?><persons> <Person> <Name> Liu Bei </Name> <Age>28</Age> </Person> <Person> <Name> Guan Yu </Name> < Age>27</age> </Person></Persons>
Quite simply, here is a very comprehensive example, including most of the node types of XML.
Class Program {static void Main (string[] args) {//Create processing instruction XPROCESSINGINSTRUCTI On instruction = new Xprocessinginstruction ("Xml-stylesheet", "href=\" hello.css\ "type = \" Text/css\ ""); Create a Claim object xdeclaration xdeclaration = new Xdeclaration ("1.0", "GB2312", "yes"); Create Document type Xdocumenttype DocumentType = new Xdocumenttype ("person", NULL, "PERSON.DTD", null); Create Xmlcdata Data Xcdata the new Xcdata ("The above code output XML is as follows:
<?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