Export (0) Print
Expand All

XPathDocument Constructor (XmlReader, XmlSpace)

.NET Framework 1.1

Initializes a new instance of the XPathDocument class.

[Visual Basic]
Public Sub New( _
   ByVal reader As XmlReader, _
   ByVal space As XmlSpace _
)
[C#]
public XPathDocument(
   XmlReader reader,
 XmlSpace space
);
[C++]
public: XPathDocument(
   XmlReader* reader,
 XmlSpace space
);
[JScript]
public function XPathDocument(
   reader : XmlReader,
 space : XmlSpace
);

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 white space.

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 white space 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

[Visual Basic, C#, C++] The following example creates an XPathDocument. The XPathDocument is constructed using an XmlTextReader which holds XML data returned from a SQL Server.

[Visual Basic] 
' Load data returned from SQL Server into an XmlTextReader.
Dim mySelectQuery as string = "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"

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

[C#] 
// 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);

[C++] 
// Load data returned from SQL Server into an XmlTextReader.
String* mySelectQuery = S"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 = S"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 = dynamic_cast<XmlTextReader*>(sqlComm -> ExecuteXmlReader());

// Instantiate an XPathDocument using the XmlTextReader.  
XPathDocument* doc = new XPathDocument(reader, XmlSpace::Preserve);

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

XPathDocument Class | XPathDocument Members | System.Xml.XPath Namespace | XPathDocument Constructor Overload List | SqlCommand | SqlConnection

Show:
© 2014 Microsoft