XML in ADO.NET

XML is the behind-the-scenes foundation of ADO.NET. Data is described in the XML format, and XML representations of DataSet objects, DataTable objects, and schemas can be written from in-memory representations of data and persisted as XML text files or streams. The XmlDataDocument class allows you to work directly with in-memory representations of XML and synchronize them with a DataSet.

Retrieving XML from a SQL Server 2000 Database

Microsoft SQL Server 2000 contains built-in support for XML-based data access. Data can be retrieved from the database and read into memory in the XML format. The ADO.NET SQL Data Provider provides built-in support for retrieving XML from the database.

Retrieving an XmlReader with a SqlCommand

The SqlCommand class provides a method for retrieving data as XML. The ExecuteXmlReader returns an XmlReader object that exposes the data returned by the SqlCommand as a set of XML rows. The SQL SELECT query executed by the ExecuteXmlReader method must contain a FOR XML clause. This method is only available with the SqlCommand class and can only be used when connecting to Microsoft SQL Server 2000 or later.

The XmlReader class is analogous to the DataReader class. It provides read-only, forward-only access to the XML returned by the query. Also like the DataReader, the XmlReader requires the exclusive use of a connection.

The XmlReader exposes a Read method, similar to the Read method of a DataReader, which allows you to iterate through the nodes that are returned. Like a DataReader, the Read method advances the XmlReader to the next node of the XML stream and returns false when the last node is reached. Also like the DataReader, you must call the Read method before the first node is accessible.

Using XML with DataSets

DataSets provide methods for interacting with data stored as XML. You can load data stored as an XML file or stream into a DataSet or write the data represented in a DataSet to an XML file or stream. You can create typed DataSet objects of a known structure by reading an XML schema into the DataSet, and you can create a template for other typed DataSets by writing the structure of the DataSet to an XML schema.

Reading XML into a DataSet

You can access XML data stores by using the DataSet.ReadXml method. This method allows you to specify an existing XML file or stream, or an existing XmlReader or TextReader object, and read the schema and data represented therein into a DataSet.
  1. To read XML into a DataSet
  2. Call the DataSet.ReadXml method.
  3. To read an XML schema into a DataSet
  4. Call the DataSet.ReadXmlSchema method.
  5. Writing XML from a DataSet
DataSet objects can write the data they contain and the schema that describes the data as XML files. The DataSet object provides the WriteXml method to facilitate exporting data to an XML format. The WriteXml method allows you to specify a file, a stream, or an XmlWriter or TextWriter object to receive the XML output from the DataSet. The following example demonstrates how to write the contents of a DataSet to an XML file called myXml.xml. If the specified file is not present, it will be created automatically.

Executing XSLT Transformations

XSLT is designed to facilitate transforming XML data into different formats. For example, an XML document might be converted to HTML for display on a Web page or might be converted to a different XML format for a specialized application.

The .NET Framework provides the XslTransform class to execute XSLT transformations. To execute a transformation, the XslTransform class first needs to load a style sheet. This is a file that contains the formatting instructions for the XML data. You use the XslTransform.Load method to load the style sheet. The style sheet can be specified as either a URL that points to an XSL file that contains the style definitions or as any of a number of classes that contain an in-memory representation of the style sheet.

RELATED POST

DAY 11 OOPS INTRODUCTION

DAY 12 POLYMORPHISM

DAY 13 INHERITANCE AND POLYMORPHISM

DAY 14 EBUGGING TOOLS IN DOT NET

DAY 15 DEBUG AND TRACE IN CLASSES

DAY 16 UNIT TEST PLAN

DAY 17 EXCEPTIONS IN VISUAL STUDIO

DAY 19 ADO.NET INTRODUCTION

DAY 20 DATA ACCESSING IN DOT NET

DAY 21 DATA BASE OBJECTS


No comments:

Post a Comment