LINQ the first article

Source: Internet
Author: User

1.Linq to XML:

Namespaces: System.Xml

Class XElement

Construct an XML tree with the Xelment class:

You can construct an XML tree in your code

XML can be parsed from a variety of sources, including TextReader, text files, or Web addresses (URLs)

You can use XmlReader to populate the tree

XAttribute class

The XAttribute class represents an XML property

property is a name/value pair associated with an element

Using the properties in LINQ to XML is very similar to using elements. Their constructors are similar. The methods used to retrieve their collections are similar. The LINQ query expression for the property collection looks very similar to the LINQ query expression for the collection of elements.

The order in which attributes are added to the element is preserved. That is, when iterating through the properties, the reduced-to-attribute order is preserved.

XDocument class

The Xdocumnet class contains the information that is required for a valid XML document. These include XML declarations, processing instructions, and annotations.

If you want the XDocument class to provide special functionality, you only need to create a XDocument object. In many cases, you can use XElement directly. Direct use of XElement is a relatively simple programming model.

The XDocument object can have only one child XElement node. This reflects the XML standard, that is, there can be only one root element in an XML document.

(vs Automatic Code finishing ctrl+a+k+f,a,k,f to take turns, don't press at the same time, a is for all the code, you can check the code you want to organize, and then K,f)

XML names are often the cause of XML programming complexity, and XML names consist of an XML namespace (also an XML namespace URI) and a local name. The role of XML namespaces is similar to the role of namespaces in programs that give. NET framework, which uniquely qualifies the names of elements and attributes. This helps prevent the XML from asking about the name collisions between the various parts. After declaring the XML namespace, you can choose a local name that only needs to be unique within this namespace.

Serializing an XML tree

The following methods in the XElement and XDocument classes can be used to serialize an XML tree. The XML tree can be serialized as a file, TextReader, or XmlReader. The ToString method is serialized as a string

Xelement.save

Xdocument.save

Xelement.tostring

Xdocument.tostring

SaveOptions

Disableformatting: Preserve insignificant whitespace when serializing

None: Do not process the format

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.IO;4 usingSystem.Linq;5 usingSystem.Text;6 usingSystem.Threading.Tasks;7 usingSystem.Xml;8 usingSystem.Xml.Linq;9 Ten namespaceLinqconsole One { A     class Program -     { -         Static voidMain (string[] args) the         { -             #regionDemo1 -             //XDocument contacts = new XDocument ( -             //new XElement ("Contacts", +             //New Xcomment ("This is a comment."), -             //new Xprocessinginstruction ("Xml-stylesheet", +             //"href= ' mystyle.css ' title= ' Compact ' type= ' text/css '"), A             //New XElement ("Contact", at             //new XElement ("Name", -             //new XAttribute ("Type", "Name"), "Patrick Hines"), -             //new XElement ("Phone", "206-201-0250"), -             //new XElement ("Address", -             //new XElement ("Street", "123 Main St"), -             //New XElement ("City", "Mercer Island"), in             //New XElement ("state", "WA"), -             //new XElement ("Postal", "52640") to             //               ) +             //               ), -             //New Xcomment ("This is another conment.") the  *             //           ) $             //       );Panax Notoginseng  -             //Contacts. Declaration = new Xdeclaration ("1.0", "Utf-8", "true"); the             //Console.WriteLine (contacts); +  A             //Contacts. Save ("Test.xml"); the             //Console.readkey (); +             #endregion -  $             #regionDemo2 $             //XElement srctree = -             //new XElement ("Root", -             //new XElement ("XElement", 1), the             //new XElement ("XElement", 2), -             //new XElement ("XElement", 3),Wuyi             //new XElement ("XElement", 4), the             //new XElement ("XElement", 5) -             //       ); Wu  -             //XElement xmlTree = About             //new XElement ("Root", $             //New XElement ("Child", 1), -             //New XElement ("Child", 2), -             //From item in srctree.elements () -             //where (int) item>2 A             //Select Item +             //      ); the  -             //Console.WriteLine (xmlTree); $             //Console.readkey (); the             #endregion the  the             #regionDemo3 the             //XmlReader r = xmlreader.create ("books.xml"); -             //While (r.nodetype! = xmlnodetype.element) in             //{ the             //R.read (); the             //} About             //XElement e1 = Xelement.load (r);//You can also load the document directly the             //Console.WriteLine (E1); the             //Console.readkey (); the             #endregion +  -             #regionDemo4 the             //XNamespace aw = "http://www.adventure-works.com";Bayi             //XNamespace fc = "www.fourthooffee.com"; the             //XElement root = new XElement ("root", the             //New XAttribute (Xnamespace.xmlns + "aw", "http://www.adventure-works.com"), -             //New XAttribute (xnamespace.xmlns+ "FC", "www.fourthooffee.com"), -             //New XElement (FC + "Child", the             //New XElement (aw+ "Differentchild", "other content") the             //    ), the             //New XElement (aw+ "Child2", "C2 content"), the             //New XElement (fc+ "Child3", "C3 content") -             //); the             //Console.WriteLine (root); the             //Console.readkey (); the             #endregion94  the             #regionDemo5 the             //serialization with XML declarations the             //XElement root = new XElement ("root",98             //New XElement ("Child", "child Content"); About             //Root. Save ("Root.xml", saveoptions.disableformatting); -             //string str = File.readalltext ("Root.xml");101             //Console.WriteLine (str);102             //Console.readkey ();103 104             //serialization without XML declaration theStringBuilder SB =NewStringBuilder ();106XmlWriterSettings Xws =Newxmlwritersettings ();107Xws. Omitxmldeclaration =true;108             using(XmlWriter XW =xmlwriter.create (SB, XWS))109             { theXElement ROOT1 =NewXElement ("Root",111                     NewXElement (" Child","Child Content")); the root1. Save (XW);113             } the  the Console.WriteLine (sb.) ToString ()); the Console.readkey ();117             #endregion118 119         } -     }121}
View Code

LINQ the first article

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.