ADO.NET and XML

ADO.NET and XML

Let's take a moment to examine the XML information generated by our data set. Rather than trying to wade through all of the records, I'm simply going to modify our SELECT command to retrieve a single record. There is no need for you to do this, but we can examine the XML output a bit easier this way.

'SqlSelectCommand1 ' Me.SqlSelectCommand1.CommandText = _ "SELECT CategoryID, CategoryName, Description " & _ "FROM Categories Where CategoryID = '1'" Me.SqlSelectCommand1.Connection = Me.SqlConnection1

Because we created the DataSet object automatically, the default name DataSet1 is provided. If you want to change the name, simply rename the DataSetName property in the data set properties dialog box. If you decide to hand-code a DataSet instead of having the wizard add one as we did, the default name will be NewDataSet. However, the DataSet constructor is overloaded. If you want to give the data set a unique name, you do this when you instantiate the DataSet object. Give it a name with no spaces when you instantiate it.

Dim dsDataSet As DataSet = New DataSet("ADO_Example")

The XML file will be created with the name you give it.

<ADO_Example> <Customers>  </Customers> </ADO_Example>

Examining Our Program's XML Output

The following is an XML file generated from a single returned record and displayed on the XML tab. Spend a moment looking at this XML file. As you can see, it's pretty straightforward to understand. The data is structured and presented hierarchically. The tags are self-explanatory.

<DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd"> <Categories> <CategoryID>1</CategoryID> <CategoryName>Beverages</CategoryName> <Description>Soft drinks, coffees, teas, beers, and ales</Description> </Categories> </DataSet1>

In order to keep our XML file unique, we have an XML namespace (xmlns) that uniquely identifies our file. DataSet1.xsd is the schema file that defines the elements of our XML file.

The first, or root, element is the <Categories> tag, and all of the descendant child elements are between that tag and the closing </Categories> tag. Elements in XML are case sensitive, unlike tags in HTML. You can see that the three elements in Categories—CategoryID, CategoryName, and Description—are the three fields that were retrieved from the Categories table.

The XML Schema Output

Now that we've seen the XML file, let's spend a moment looking at the schema generated for our single record. The name, or ID, of the schema is the following:

<xsd:schema targetNamespace="http://www.tempuri.org/DataSet1.xsd" xmlns="http://www.tempuri.org/DataSet1.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified">

Notice that the target namespace is the same as our XML file, which is how the schema is associated with the XML file. Next, the root element of the schema is the name of the data set, DataSet1<xsd:element name="DataSet1" msdata:IsDataSet="true">

The xmlns:xsd element shows that the file uses the XSD namespace that conforms to the latest W3C XML schema recommendation.

xmlns:xsd="http://www.w3.org/2001/XMLSchema"

After the root name and its various elements are listed, we see that this schema is considered a complex type.

<xsd:complexType>

Complex types are user-defined data types that can include other elements or attributes. Complex types can contain elements defined as either simple or complex. Complex types can also include attributes and groups. Complex types are defined using the complexType element and typically contain combinations of element, attribute, and group declarations, as well as references to globally declared elements and groups. A complex type can be thought of as a minischema that defines the valid structure and data contained within a specific element.

You can see that the elements within this complex type are nested. The basic building blocks of XML schemas are elements and attributes. Data types define the valid content that elements and attributes contain. When you create XML schemas, you define the individual elements and attributes and assign valid types to them. The first child element, CategoryID, is a data type of String and can have zero or more occurrences of this field. A basic element definition consists of a name and a data type. Elements with the minOccurs = "0" attribute are considered optional columns. You can see that the CategoryID element is read-only, auto-incremented, and an integer data type. This information is used when a data set is reconstituted from an XML file.

<xsd:element name="CategoryID" msdata:ReadOnly="true" msdata:AutoIncrement="true" type="xsd:int" />

Taking a look at the Description element, it's easy to see that it is a String data type and optional (because it includes the attribute minOccurs="0").

<xsd:element name="Description" type="xsd:string" minOccurs="0" />

A maxOccurs="unbounded" attribute tells us that this element can occur any number of times. Many novices find the occurrences attribute confusing. Table 11-2 describes how the minOccurs and maxOccurs attributes work.

Table 11-2  minOccurs and maxOccurs Element Descriptions

minOccurs

maxOccurs

Description

0

1

The element is optional, but it can only contain one element.

1

1

There must be only a single occurrence of the element.

0

Unbounded

There can be any number of occurrences of this element.

1

Unbounded

There must be at least one occurrence of this element.

2

7

There must be at least two occurrences of this element, but no more than seven.

Toward the end of the file, the Constraint1 element was added. In this case, the constraint is showing that the CategoryID field is the primary key. The schema contains all the information needed to fully describe the content of the associated XML file.

<xsd:unique name="Constraint1" msdata:PrimaryKey="true"> <xsd:selector xpath=".//Categories" /> <xsd:field xpath="CategoryID" /> </xsd:unique>

So now you have a general sense of how an XML schema works. A complete schema contains enough information for .NET to reconstitute the format of the SQL table we retrieved, and the XML file fills in the data, as shown here.

<xsd:schema targetNamespace="http://www.tempuri.org/DataSet1.xsd" xmlns="http://www.tempuri.org/DataSet1.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified"> <xsd:element name="DataSet1" msdata:IsDataSet="true"> <xsd:complexType> <xsd:choice maxOccurs="unbounded"> <xsd:element name="Categories"> <xsd:complexType> <xsd:sequence> <xsd:element name="CategoryID" msdata:ReadOnly="true" msdata:AutoIncrement="true" type="xsd:int" /> <xsd:element name="CategoryName" type="xsd:string" /> <xsd:element name="Description" type="xsd:string" minOccurs="0" /> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:choice> </xsd:complexType> <xsd:unique name="Constraint1" msdata:PrimaryKey="true"> <xsd:selector xpath=".//Categories" /> <xsd:field xpath="CategoryID" /> </xsd:unique> </xsd:element> </xsd:schema>



Coding Techniques for Microsoft Visual Basic. NET
Coding Techniques for Microsoft Visual Basic .NET
ISBN: 0735612544
EAN: 2147483647
Year: 2002
Pages: 123
Authors: John Connell

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