[C #] Introduction to LINQ to XML-Conversion

Source: Internet
Author: User
Tags xml attribute xslt

Overview of the linq to XML class

The purpose of LINQ to XML is to make the XML name as simple as possible.

 

   
Xattribute class Xattribute indicates an XML Attribute.
Xcdata class Xcdata indicates a CDATA text node.
Xcomment class Xcomment indicates an XML comment.
Xdeclaration class Xdeclaration indicates an XML declaration. The XML declaration is used to declare the encoding of the XML version and document. In addition, the XML Declaration also specifies whether the XML document is an independent document.
Xname class Xname indicates the name of the element (xelement) and attribute (xattribute.
Xcontainer class Xcontainer is an abstract base class applicable to all nodes that may have subnodes.The following classes are derived from the xiner iner class:
  • Xelement
  • Xdocument
Xelement class Xelement indicates an XML element.
Xdocument class Xdocument indicates an XML document.
Xdocumenttype Xdocumenttype indicates an XML document type definition (DTD ).

 

XML names are generally considered as high-level topics in XML due to their complexity. There is evidence that this complexity is not caused by namespaces that are often used by developers for programming, but by namespaces prefixes. Using the namespace prefix can reduce the number of clicks required for XML input or make XML more readable. However, the prefix is usually a shortcut that uses the complete XML namespace, which is not required in most cases. By parsing all prefixes into the corresponding XML namespace, LINQ to XML simplifies the XML name. If necessary, you can use the getprefixofnamespace method to use the prefix.

You can control the namespace prefix if necessary. In some cases, if you are using another XML system (such as XSLT or XAML), you need to control the namespace prefix. For example, if an XPATH expression uses the namespace prefix embedded in An XSLT style table, make sure that the XML document is serialized using a namespace prefix that matches the prefix used in the XPath expression.

 

   
Xnamespace class Xnamespace indicates the namespace of xelement or xattribute. A namespace is a component of xname.
Xnode class Xnode is an abstract class that represents the nodes in the XML tree.
Xnodedocumentordercomparer class Xnodedocumentordercomparer provides the document Sequence Function for comparing nodes.
Xnodeequalitycomparer class Xnodeequalitycomparer provides the function to compare whether the node values are equal.
Xobject class Xobject is the abstract base class of xnode and xattribute. It provides annotation and event functions.
Xobjectchange class Xobjectchange specifies the event type when an xobject event is triggered.
   
   
   

 

 

  Xdocument class overview

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

Please note that,If you need specific functions provided by the xdocument class, you only need to create an xdocument object. In many cases, xelement can be used directly. Directly Using xelement is a simple programming model.

Xdocument is derived from xiner iner. Therefore, it can contain subnodes. However,The xdocument object can have only one xelement node. This reflects the XML standard, that is, there can be only one root element in the XML document.

  Use xelement without xdocument.

As mentioned above,The xelement class is the main class in the LINQ to XML programming interface.In many cases, you do not need to create documents for your application. By using the xelement class, you can create an XML tree, add other XML trees to it, modify the XML tree, and save it.

To construct an xdocument, you can use the function to construct it, just as you would construct an xelement object.

  

The following code creates an xdocument object and its associated inclusion objects.

XDocument d = new XDocument(   new XComment("This is a comment."),   new XProcessingInstruction("xml-stylesheet",   "href=‘mystyle.css‘ title=‘Compact‘ type=‘text/css‘"),   new XElement("Pubs",   new XElement("Book",   new XElement("Title", "Artifacts of Roman Civilization"),   new XElement("Author", "Moreno, Jordao")   ),   new XElement("Book",   new XElement("Title", "Midieval Tools and Implements"),   new XElement("Author", "Gazit, Inbar")   )   ),   new XComment("This is another comment.")   );   d.Declaration = new XDeclaration("1.0", "utf-8", "true");   Console.WriteLine(d);   d.Save("test.xml"); 

When you check the file test. XML, the following output is displayed:

<?xml version="1.0" encoding="utf-8"?><!--This is a comment.--><?xml-stylesheet href=‘mystyle.css‘ title=‘Compact‘ type=‘text/css‘?><Pubs>  <Book>    <Title>Artifacts of Roman Civilization</Title>    <Author>Moreno, Jordao</Author>  </Book>  <Book>    <Title>Midieval Tools and Implements</Title>    <Author>Gazit, Inbar</Author>  </Book></Pubs><!--This is another comment.-->

 

Xelement class overview

The element class is one of the basic classes in LINQ to XML. It represents an XML element. You can use this class to create elements, change element content, add, change, or delete child elements, add attributes to elements, or serialize Element Content in text format. It can also interoperate with other classes in system. XML (such as xmlreader, xmlwriter, and compiledtransform.

Functions provided by the xelement class.

  Construct an XML tree

You can use the following methods to construct an XML tree:

You can construct an XML tree in the code.

XML can be parsed from various sources including textreader, text file, or web address (URL.

Xmlreader can be used to fill the tree. For more information, see readfrom.

If you have a module that can write content to xmlwriter, you can use the createwriter method to create a writer, pass the writer to this module, and then use the content written to xmlwriter to fill the XML tree.

However, the most common method to create an XML tree is as follows:

XElement contacts =   new XElement("Contacts",   new XElement("Contact",   new XElement("Name", "Patrick Hines"),   new XElement("Phone", "206-555-0144"),   new XElement("Address",   new XElement("Street1", "123 Main St"),   new XElement("City", "Mercer Island"),   new XElement("State", "WA"),   new XElement("Postal", "68042")   )   )   );

Another common method for creating an XML tree is to fill the XML tree with the results of the LINQ query, as shown in the following example:

XElement srcTree = new XElement("Root",   new XElement("Element", 1),   new XElement("Element", 2),   new XElement("Element", 3),   new XElement("Element", 4),   new XElement("Element", 5)   );   XElement xmlTree = new XElement("Root",   new XElement("Child", 1),   new XElement("Child", 2),   from el in srcTree.Elements()   where (int)el > 2   select el   );   Console.WriteLine(xmlTree);

This example generates the following output:

<Root>  <Child>1</Child>  <Child>2</Child>  <Element>3</Element>  <Element>4</Element>  <Element>5</Element></Root>

 

[C #] Introduction to LINQ to XML-Conversion

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.