Using an XmlReader
Use an XmlReader obtained by calling the ExecuteXmlReader method of the SqlCommand object when:
- You want to process the retrieved data as XML, but you do not want to incur the performance overhead of creating a DataSet and do not require a disconnected cache of data.
- You want to exploit the functionality of the SQL Server 2000 FOR XML clause, which allows XML fragments (that is, XML documents with no root element) to be retrieved from the database in a flexible manner. For example, this approach lets you specify precise element names, whether an element or attribute-centric schema should be used, whether a schema should be returned with the XML data and so on.
If you use the XmlReader, note the following:
The connection must remain open while you read data from the XmlReader. The ExecuteXmlReader method of the
SqlCommand object currently does not support the CommandBehavior.CloseConnection enumerated value, so you must
explicitly close the connection when you finish with the reader