XML Data Access

Team-Fly    

 
Application Development Using Visual Basic and .NET
By Robert J. Oberg, Peter Thorsteinson, Dana L. Wyatt
Table of Contents
Chapter 13.  Programming with ADO.NET


As we will discuss in Chapter 15, XML has many advantages for describing data that must move between heterogeneous systems and data sources. Since you can validate your XML against an XML schema description, you can pass it in many situations where passing a DataSet makes no sense. [15] Since XML is text, it can pass through firewall ports that are normally open, unlike the DCOM or RMI protocols that require special ports to be open.

[15] When you remote a DataSet , it is remoted as XML; nonetheless, if you have to interact with an unmanaged program, you can convert the data in the DataSet to XML and send it. As discussed in the Chapter 15, the XML protocol used by remoting and Web services is not identical.

The thrust of these next sections is not to discuss XML in any great detail. We just want to demonstrate how you can move back and forth between looking at data in XML and looking at data with a DataSet .

XML Schema and Data

XML does not dictate how data is organized or what the meaning of XML documents are. It only describes the rules on how the documents are put together. [16] An XML schema describes the metadata of how the data is organized inside an XML document. XML schemas are written in XML.

[16] Technically speaking, XML documents in the sense that we speak of are defined by the XML Infoset and consist of documents, elements, and attributes.

For example, XML can be used to describe data in a relational database, but an XML schema can be used to describe relationships such as primary and foreign keys. Having the XML schema and the data in one document or text stream is vastly simpler than having to download each table into a dataset and then programmatically set up the relations between the tables.

XmlDataDocument

Documents can include database output within them. For example, a sales report has an explanation as well as the sales data that was pulled from a data source. The XmlDataDocument class can be used to represent data in the form of an XML document.

The XmlDataDocument class inherits from XmlDocument , which represents an XML document for the .NET XML Framework classes. What makes the XmlDataDocument particularly interesting is that you can construct an XmlDataDocument from a DataSet by passing the DataSet instance to the XmlDataDocument constructor. The XmlDataDocument has a read-only DataSet property so that you can work with the XML document as relational data if that makes sense.

DataSet and XML

The DataSet has methods , WriteXml and WriteXmlSchema , that can write out the data and schema associated with the dataset. The XML schema that the DataSet writes out is deduced from the current set of tables, columns , constraints, and relations. Unless you explicitly add the constraints to the DataSet , such as primary key or foreign key relationships, they will not be part of the schema.

The DataSet also has methods to read XML: ReadXml and ReadXmlSchema . ReadXml can read both the data and the schema into the dataset. If a schema is not present, it will try to infer one from the data. If it cannot infer a schema, it will throw an exception. ReadXmlSchema will read in a schema document.

If there is no schema in an XML document, the DataSet extracts elements that would be defined as tables according to a set of rules. The remaining elements, along with the attributes, are then assigned as columns to the tables.

You can use the ColumnMapping property of the DataColumn class to control whether you want columns written as XML elements or attributes. Elements that are not scalar values become tables; attributes and scalar values are columns. The exact procedure is described in the .NET documentation.


Team-Fly    
Top
 


Application Development Using Visual BasicR and .NET
Application Development Using Visual BasicR and .NET
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 190

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net