[Post] About xmldocument and xpathdocument

Source: Internet
Author: User

The system. xml namespace uses the xmldocument or xpathdocument class to provide the programming representation of XML documents, fragments, nodes, or node sets in memory.

The xpathdocument class uses the XPath data model to provide a fast, read-only representation of XML documents in memory. The xmldocument class provides an editable representation of XML documents that implement W3C Document Object Model (DOM) Level 1 core and core Dom level 2 in memory. Both classes implement the ixpathnavigable interface and return the xpathnavigator object, which is used to select, compute, browse, and (in some cases) EDIT basic XML data.

Use the xpathdocument class to read XML documents

The xpathdocument class uses the XPath data model to provide a fast, read-only representation of XML documents in memory. An instance of the xpathdocument class is created using one of its six constructors. Through these constructor, you can use stream, textreader, xmlreader object, and the string path of the XML file to read the XML document.

Use the xmldocument class to read XML documents

Xmldocument class is an editable representation of XML documents that implement W3C Document Object Model (DOM) Level 1 core and core Dom level 2 in memory. An instance of the xmldocument class is created using one of its three constructors. You can create a new empty xmldocument object by calling the xmldocument class constructor without parameters. After the constructor is called, load the XML data from the stream, textreader, or xmlreader object to the new xmldocument object using the load method and the string path of the XML file.

Determine the encoding of the XML document

Xmlreader objects can be used to read XML documents and create xpathdocument and xmldocument objects, as shown in the previous sections. However, xmlreader objects may read unencoded data and therefore do not provide any encoding information.

The xmltextreader class inherits from the xmlreader class, uses its encoding attribute to provide encoding information, and can be used to create an xpathdocument object or xmldocument object.

For more information about the encoding information provided by the xmltextreader class, see the encoding attribute in the xmltextreader class reference document.

Create an xpathnavigator object

After reading an XML document into an xpathdocument or xmldocument object, you can create an xpathnavigator object to select, compute, browse, and (in some cases) EDIT basic XML data.

In addition to the xmlnode class, the xpathdocument and xmldocument classes also implement the ixpathnavigable interface of the system. xml. XPath namespace. Therefore, all three classes provide the createnavigator method that returns the xpathnavigator object.

Use the xpathnavigator class to edit the XML document

In addition to selecting, computing, and browsing XML data, the xpathnavigator class can also be used to edit XML documents in some cases, depending on the object in which the document is created.

The xpathdocument class is read-only, while the xmldocument class is editable. Therefore, the xpathnavigator object created through the xpathdocument object cannot be used to edit XML documents, but it can be created through the xmldocument object. The xpathdocument class is only used to read XML documents. The xmldocument class should be used if you need to edit an XML document or access other functions provided by the xmldocument class (such as event processing.

The canedit attribute of the xpathnavigator class specifies whether the xpathnavigator object can edit XML data.

The following table describes the values of the canedit attribute of each class.

Ixpathnavigable implementation Canedit Value

Xpathdocument

False

Xmldocument

True

The xpathnavigator class provides a set of methods for selecting a node set in an xpathdocument or xmldocument object using an XPATH expression. After selection, you can access the selected node set cyclically.

Xpathnavigator Selection Method

The xpathnavigator class provides a set of methods for selecting a node set in an xpathdocument or xmldocument object using an XPATH expression. The xpathnavigator class also provides a set of optimized methods to select higher-level nodes, subnodes, and child nodes faster than using XPath expressions. If you select a single node, the selected node set is returned in the xpathnodeiterator object or xpathnavigator object.

Select a node using an XPATH expression

To use an XPATH expression to select a node set, use one of the following methods.

  • Select

  • Selectsinglenode

If you select a single node during the call, these methods return a group of nodes. You can use the xpathnodeiterator object or the xpathnavigator object to browse at will.

Browsing through the xpathnodeiterator object does not affect the location of the xpathnavigator object used to create the object. The xpathnavigator object returned from the selectsinglenode method is located on a single returned node and does not affect the location of the xpathnavigator object used to create the object.

The following example shows how to create an xpathnavigator object through the xpathdocument object, how to use the select method to select nodes in the xpathdocument object, and how to use the xpathnodeiterator object to access the selected nodes cyclically.

<Bookstore>
<Book genre = "autobiography" publicationdate = "1981-03-22" ISBN = "1-861003-11-0">
<Title> The Autobiography of Benjamin Franklin </title>
<Author>
<First-name> Benjamin </first-name>
<Last-name> Franklin </last-name>
</Author>
<Price> 8.99 </price>
</Book>
<Book genre = "novel" publicationdate = "1967-11-17" ISBN = "0-201-63361-2">
<Title> the confidence man </title>
<Author>
<First-name> Herman </first-name>
<Last-name> Melville </last-name>
</Author>
<Price> 11.99 </price>
</Book>
<Book genre = "Philosophy" publicationdate = "1991-02-15" ISBN = "1-861001-57-6">
<Title> the gorgias </title>
<Author>
<Name> Plato </Name>
</Author>
<Price> 9.99 </price>
</Book>
</Bookstore>

Xpathdocument document = new xpathdocument ("books. xml ");
Xpathnavigator navigator = Document. createnavigator ();
Xpathnodeiterator nodes = navigator. Select ("/bookstore/book ");

While (nodes. movenext ())
{
Console. writeline (nodes. Current. Name );
}

Optimized Selection Method

The selectchildren, selectancestors, and selectdescendants methods of the xpathnavigator class indicate the XPath expressions used to retrieve subnodes, child nodes, and parent nodes. The performance of these methods has been optimized, which is faster than the corresponding XPath expression. The selectchildren, selectancestors, and selectdescendants Methods Select upper-level nodes, subnodes, and Child Nodes Based on the xpathnodetype value or the local name and namespace URI of the node to be selected. The selected parent node, child node, and child node are returned in the xpathnodeiterator object.

The xpathnavigator class provides two groups of browsing methods. The first group is used to browse the node set in the xpathnavigator node set to locate the topic. The second group (described in this topic) is used to browse attributes and namespace nodes in xpathdocument or xmldocument objects.

Browse attribute nodes

An attribute is an attribute of an element, not a child of an element. This difference is important because the xpathnavigator class used to browse peer nodes, parent nodes, and child nodes has different methods.

For example, the movetoprevious and movetonext methods do not need to browse from elements to properties or between properties. Properties use different browsing methods.

The following describes how to browse attributes of the xpathnavigator class.

  • Movetoattribute

  • Movetofirstattribute

  • Movetonextattribute

When the current node is an element, you can use the hasattributes method to check whether any attributes associated with the element exist. If an element is known to have attributes, you can access these attributes in multiple ways. To retrieve a single attribute from an element, use the getattribute method. To move xpathnavigator to a specific attribute, use the movetoattribute method. You can also cyclically access each attribute of an element by first using the movetofirstattribute method and then calling the movetonextattribute method multiple times.

Note:

If the xpathnavigator object is located on an attribute or namespace node, the movetochild, movetodescendant, movetofirst, movetofirstchild, parent, movetoid, movetonext, and movetoprevious methods always return false, which does not affect the X. The moveTo, movetoparent, and movetoroot methods are excluded.

XML files can be represented in Microsoft. NET Framework in different ways. Including using string, or using xmlreader, xmlwriter, xmldocument, or xpathdocument classes. To facilitate switching between the representation of these different XML documents, the xpathnavigator class provides many methods and attributes for extracting XML as a string, xmlreader object, or xmlwriter object.

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.