System.Xml.XPath.XPathDocument Class

Assembly: System.Xml.dll
Namespace: System.Xml.XPath
Summary
Provides a fast and performant read-only cache for XML document processing using XSLT.
C# Syntax:
public class XPathDocument : IXPathNavigable
Remarks
This class is optimized for XSLT processing and the XPath data model. It does not maintain node identity nor does it do the rule checking required by the W3C DOM.
See also:
System.Xml.XPath Namespace | XslTransform

System.Xml.XPath.XPathDocument Member List:

Public Constructors
ctor #1 Overloaded:
.ctor(Stream stream)

Initializes a new instance of the XPathDocument class.
ctor #2 Overloaded:
.ctor(string uri)

Initializes a new instance of the XPathDocument class.
ctor #3 Overloaded:
.ctor(TextReader reader)

Initializes a new instance of the XPathDocument class.
ctor #4 Overloaded:
.ctor(XmlReader reader)

Initializes a new instance of the XPathDocument class.
ctor #5 Overloaded:
.ctor(string uri, XmlSpace space)

Initializes a new instance of the XPathDocument class.
ctor #6 Overloaded:
.ctor(XmlReader reader, XmlSpace space)

Initializes a new instance of the XPathDocument class.
Public Methods
CreateNavigator Creates an XPathNavigator for navigating this document.
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Xml.XPath.XPathDocument Member Details

Overloaded ctor #1
Summary
Initializes a new instance of the XPathDocument class.
C# Syntax:
public XPathDocument(
   Stream stream
);
Parameters:

stream

The stream containing the data to load.

Exceptions
Exception Type Condition
XmlException There is a fatal error in the XML. In this case, the XPathDocument remains empty.

Return to top


Overloaded ctor #2
Summary
Initializes a new instance of the XPathDocument class.
C# Syntax:
public XPathDocument(
   string uri
);
Parameters:

uri

A URI that specifies a file containing the data to load.

Exceptions
Exception Type Condition
XmlException There is a fatal error in the XML. In this case, the XPathDocument remains empty.
Remarks
To preserve white space, use the overload that accepts an XmlSpace as an argument.

The following C# code creates an XPathDocument.

              XPathDocument doc = new XPathDocument("data.xml");
            

Return to top


Overloaded ctor #3
Summary
Initializes a new instance of the XPathDocument class.
C# Syntax:
public XPathDocument(
   TextReader reader
);
Parameters:

reader

A TextReader containing the data to load.

Exceptions
Exception Type Condition
XmlException There is a fatal error in the XML. In this case, the XPathDocument remains empty.

Return to top


Overloaded ctor #4
Summary
Initializes a new instance of the XPathDocument class.
C# Syntax:
public XPathDocument(
   XmlReader reader
);
Parameters:

reader

An XmlReader containing the data to load.

Exceptions
Exception Type Condition
XmlException There is a fatal error in the XML. In this case, the XPathDocument remains empty.
Remarks
If the XmlReader is positioned on a node, the data is loaded from the current position of the XmlReader through all its siblings. This enables you to load only a portion of a document.

If the XmlReader is positioned on a leaf node that is invalid for the root level of a document, for example a whitespace or attribute node, the reader continues to read until it is positioned on a node that can be used for the root. The document begins loading at this point.

The XPathDocument can be used to load an XML fragment. If the XML fragment includes multiple sibling nodes, XPathDocument creates a root node and encapsulates the fragment nodes. Any white space found at the root level of the document or fragment is ignored. To preserve white space, use the overload that accepts an XmlSpace as an argument.

Example
The following example loads the third book node of the books.xml file into the XPathDocument.
    XmlTextReader reader = new XmlTextReader("books.xml");
    reader.MoveToContent();
    reader.Read();
    reader.Skip(); // Skip the first book.
    reader.Skip(); // Skip the second book.

    // Create the document.
    XPathDocument doc = new XPathDocument(reader);

    

Return to top


Overloaded ctor #5
Summary
Initializes a new instance of the XPathDocument class.
C# Syntax:
public XPathDocument(
   string uri,
   XmlSpace space
);
Parameters:

uri

A URI that specifies a file containing the data to load.

space

An XmlSpace value indicating whether to preserve whitespace. Setting this to XmlSpace.Default preserves only significant white space; XmlSpace.Preserve preserves all white space.

Return to top


Overloaded ctor #6
Summary
Initializes a new instance of the XPathDocument class.
C# Syntax:
public XPathDocument(
   XmlReader reader,
   XmlSpace space
);
Parameters:

reader

An XmlReader containing the data to load.

space

An XmlSpace value indicating whether to preserve white space. Setting this to XmlSpace.Default preserves only significant white space; XmlSpace.Preserve preserves all whitespace.

Exceptions
Exception Type Condition
XmlException There is a fatal error in the XML. In this case, the XPathDocument remains empty.
Remarks
If the XmlReader is positioned on a node, the data is loaded from the current position of the XmlReader through all its siblings. This enables you to load only a portion of a document.

If the XmlReader is positioned on a leaf node that is invalid for the root level of a document, for example a whitespace or attribute node, the reader continues to read until it is positioned on a node that can be used for the root. The document begins loading at this point.

The XPathDocument can be used to load an XML fragment. If the XML fragment includes multiple sibling nodes, XPathDocument creates a root node and encapsulates the fragment nodes.
Example
The following example creates an XPathDocument. The XPathDocument is constructed using an XmlTextReader which holds XML data returned from a SQL Server.
  // Load data returned from SQL Server into an XmlTextReader.
  string mySelectQuery = "SELECT Customers.CompanyName, Orders.OrderID FROM Customers " +
                         "INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID " +
                         "WHERE CompanyName LIKE 'E%' FOR XML AUTO, XMLDATA, " +
                         "ELEMENTS, BINARY BASE64";

  string myConnectString = "server=localhost;Integrated Security=True;database=northwind";
  SqlConnection sqlConn = new SqlConnection( myConnectString );
  sqlConn.Open();
 
  SqlCommand sqlComm = new SqlCommand( mySelectQuery , sqlConn);
  sqlComm.CommandTimeout = 15;
  XmlTextReader reader = (XmlTextReader)sqlComm.ExecuteXmlReader( ); 
 
  // Instantiate an XPathDocument using the XmlTextReader.  
  XPathDocument doc = new XPathDocument(reader, XmlSpace.Preserve);

    
See also:
SqlCommand | SqlConnection

Return to top


Method: CreateNavigator()
Summary
Creates an XPathNavigator for navigating this document.
C# Syntax:
public XPathNavigator CreateNavigator();
Return Value:
An XPathNavigator object.
Implements:
IXPathNavigable.CreateNavigator
Remarks
The XPathNavigator is positioned at the root of the document. This method is part of the IXPathNavigable interface.

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~XPathDocument();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.