Creating the Data Sef s Schema

Although there are many ways to create a DataSet schema, there are two smart ways I want to show you to help you get the job done quickly. The first method involves using an already loaded DataSet. If you have a DataSet loaded with the proper data elements and relationships, all you need to do to create a schema to "lock-in" this DataSet's format is to use the WriteXmlSchema method like this:

Dim XMLOut As IO.FileStream = New IO.FileStream("c:\XMLSchema.xsd",

IO.FileMode.OpenOrCreate, IO.FileAccess.Write) dsCompany.WriteXmlSchema(XMLOut)

This code creates a schema on your hard drive that you can then load into future DataSets to enforce the format you worked so hard to develop the first time around. Notice that the output file I am creating ends in a .xsd extension, which represents an XML schema definition. For a DataSet containing an Employees and Offices table, the schema contained in this file would look like this:

<xsd:schema id="Company" targetNamespace="" xmlns="" xmlns:xsd=_

"http://www.w3.org/2 0 01/XMLSchema" xmlns:msdata="urn:schemas-microsoft-_

com: xml-msdata">

<xsd:element name="Company" msdata:IsDataSet="true">

<xsd:complexType>

<xsd:choice maxOccurs="unbounded">

<xsd:element name="Employees">

<xsd:complexType>

<xsd:sequence>

<xsd:element name="EmpID" type="xsd:int" min0ccurs="0" /> <xsd:element name="EmpName" type="xsd:string" min0ccurs="0"

<xsd:element name="OfficeID" type="xsd:int" min0ccurs="0" /> <xsd:element name="HireDate" type="xsd:string" _ min0ccurs="0" /> </xsd:sequence> </xsd:complexType> </xsd:element>

<xsd:element name="Offices"> <xsd:complexType> <xsd:sequence>

<xsd:element name="OfficeID" type="xsd:int" min0ccurs="0" /> <xsd:element name="OfficeName" type="xsd:string" _

min0ccurs="0" /> <xsd:element name="OfficeSym" type="xsd:string" _ min0ccurs="0" /> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:choice> </xsd:complexType> <xsd:unique name="Constraint1"> <xsd:selector xpath=".//Employees" /> <xsd:field xpath="OfficeID" /> </xsd:unique>

<xsd:keyref name="EmpOffice" refer="Constraint1"> <xsd:selector xpath=".//Offices" /> <xsd:field xpath="OfficeID" /> </xsd:keyref> </xsd:element> </xsd:schema>

The second method for creating data schemas should be used when you do not have a perfected DataSet available to dump a schema from. Database developers will like this second method because they get to use a nifty graphical tool to plan and design the schema, much like creating a database schema in SQL Server. Visual Studio .NET allows you to add an item named the XML Schema item to your projects. You can edit this item in one of two modes: Designer and XML. The Designer mode allows you to drag and drop items from the XML Schema panel of the Toolbox over to your schema's surface, and then configure these items in a graphical way. The XML editing mode allows you to manipulate the actual text behind the schema, which is a pretty complicated way to work with XML.

Within the XML Schema panel, you will see designer components that you can use to add elements, attributes, complex and simple types, keys, and relationships. Looking back to the schema example generated from the Employees and Offices tables, you can see that the Employees and Offices tables translate into two different complex types. The attributes to these tables are elements within the schema, listed within their associated tables/complex types. A simple type can be used to create a customized data type, such as one you call Age. After naming a simple type, you can define characteristics about it, such as its length and ability to support white spaces. Using the XML Designer tool requires a certain degree of knowledge of XML tagging and its supported data types, so if you are new to the world of XML, you might want to review Chapters 5 and 14 before trying out this tool.

0 0

Post a comment