ArticleDirectory
- Overview of the linq to XML class
LINQ to XML learning-2. Overview of LINQ to XML programming overview of the linq to XML class overview of the xattribute class
Xattribute indicates an XML Attribute.
Xcdata class
Xcdata indicates a CDATA text node.
Xcomment class
Xcomment indicates an XML comment.
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:
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.
Xdocument class
Xdocument indicates an XML document.
Xdocumenttype
Xdocumenttype indicates an XML document type definition (DTD ).
Xelement class
Xelement indicates an XML element. For details and examples,
Xname class
Xname indicates the name of the element (xelement) and attribute (xattribute. For details and examples,
The purpose of LINQ to XML is to make the XML name as simple as possible. 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. The following classes are derived from the xnode class:
Xtext
Xcontainer
Xcomment
Xprocessinginstruction
Xdocumenttype
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.
Xobjectchangeeventargs class
Xobjectchangeeventargs provides data for changing and changed events.
Xprocessinginstruction class
Xprocessinginstruction indicates an XML Processing Instruction. The Processing Command passes the information to the XML processing application.Program.
Xtext
Xtext indicates a text node. This type is not required in most cases. This class is mainly used for mixed content.
Xdocument class overview
The xdocument class contains the information required for a valid XML document. These include XML declarations, processing instructions, and annotations.
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 followingCodeCreate an xdocument object and its associated contained objects.
Xdocument d = New Xdocument ( New Xcomment (" This is a comment. "), New Xprocessinginstruction ("XML-stylesheet "," Href='mystyle.css 'Title = 'comput' 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" ?>
XML-stylesheet hrefw.'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>
Xelement class overview
The xelement 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>