Working with Schemas


XML schemas play a major role in the .NET Framework, and Visual Studio .NET (VS .NET) provides many tools and utilities to work with XML. The .NET Framework uses XML to transfer data from one application to other. XML schemas define the structure and validation rules of XML documents. You use XML Schema Definition (XSD) language to define XML schemas.

VS .NET provides an XML designer to work with schemas. In this section, you'll learn how to take advantage of the VS .NET XML designer and wizards to work with XML documents and databases.

Generating a New Schema

To generate a new schema, create a new Windows application using File New Project Visual Basic Projects Windows Application. Then just follow the steps outlined in the following sections.

Step 1: Adding an Empty Schema

You can add an XML schema to a project by right-clicking a project and then selecting Add Add New Item. The Add New Item option opens the Add New Item dialog box, where you can select different templates. To add an XML schema, you need to select the XML Schema template on this page. This page also allows you to specify the schema name. The default schema name is XMLSchema1.xsd. Next, click the Open button on the Add New Item page.

This action launches the XML designer, which allows you to design XML schemas. The default XML designer page like Figure 6-8. As you can see, there are two links available: Server Explorer and Toolbox.

click to expand
Figure 6-8: The XML designer

As you might guess, the Server Explorer link launches the Server Explorer. (See Chapter 2 to learn more about the Server Explorer.) The Toolbox link launches a Toolbox that contains the XML Schema items rather than Windows or Web controls.

If you click the XML option at the bottom of screen, you'll see that your XML looks like the following:

 <?xml version="1.0" encoding="utf-8" ?> <xs:schema  targetNamespace="http://tempuri.org/XMLSchema1.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema1.xsd" xmlns:mstns="http://tempuri.org/XMLSchema1.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema"> </xs:schema> 

Step 2: Adding Schema Items

Once an empty schema is available in the XML designer, you can click the Toolbox link to open the Toolbox and add schema items (see Figure 6-9).


Figure 6-9: The XML schema Toolbox

The XML designer works in pretty much same way as the form designer. You drag components from the Toolbox to the designer, and the designer writes the code for you. As you can see in Figure 6-9, you can add an element, attribute, complexType, and other schema items to the form.

First, let's add an element to the schema, dragging it from the Toolbox. Second, set its name and type in the designer. The default element looks like Figure 6-10. If you click the right-side column of the grid, you'll see a drop-down list with element types. You can either select the type of an item from the list or define your own type (which is a user-defined type).


Figure 6-10: Adding a schema element and its type

For this example, define your own custom type. Define the first element as bookstore with a custom type of bookstoretype. Figure 6-11 shows the bookstore element of bookstoretype.

click to expand
Figure 6-11: Adding a new bookstore element

Now add a complexType item by dragging a complexType item to the XML designer (see Figure 6-12).


Figure 6-12: A complexType item

A complexType item can contain other types, too. You can add items to a complexType in many ways. You can either drag an item from the Toolbox to the complexType or right-click a complexType and use the Add option and its suboptions to add an item. Figure 6-13 shows different items you can add to a complexType.

click to expand
Figure 6-13: Adding other types to complexType item

You can delete items by right-clicking and selecting Delete. You can also delete the entire complexType or other schema items by right-clicking the header of an item or right-clicking the left side of the item.

Now, rename the added complexType name to book and add four element types: title, author, price, and category. Now your complexType book should now look like Figure 6-14.


Figure 6-14: The book complexType and its elements

Next, add one more complexType author with two elements: first-name and last-name. Your final schema should now look like Figure 6-15.

click to expand
Figure 6-15: The author and book complexTypes in an XML schema

You can now see the XML code for this schema by clicking the left-bottom XML button (see Figure 6-16).

click to expand
Figure 6-16: Viewing the XML code for a schema

You just saw how the VS .NET Integrated Development Environment (IDE) provides a tool to design your custom schema.

Adding Schema from a Database Objects

You can generate a typed DataSet (discussed in Chapter 4)—which actually is an XML schema—from a database table or other objects such as a view or a stored procedure. To test this option, you use the Server Explorer, which you can launch either by clicking the Server Explorer option of the XML designer or by opening it from the View Server Explorer menu item.

To create an XML schema from a database table, you simply open the Server Explorer, expand a database table, and drag a table onto the XML schema designer. For this example, drag the Employees table onto the designer. After dragging it, the XML designer generates a schema for the table (see Figure 6-17).


Figure 6-17: XML designer–generated schema

Listing 6-28 shows the generated XML code.

Listing 6-28: The XML Schema Generated for a Database Table

start example
 <?xml version="1.0" encoding="utf-8" ?> <xs:schema  targetNamespace= "http://tempuri.org/XMLSchema1.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema1.xsd" xmlns:mstns="http://tempuri.org/XMLSchema1.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> <xs:element name="Document"> <xs:complexType> <xs:choice maxOccurs="unbounded"> <xs:element name="Employees"> <xs:complexType> <xs:sequence> <xs:element name="EmployeeID" msdata:ReadOnly="true" msdata:AutoIncrement="true" type="xs:int" /> <xs:element name="LastName" type="xs:string" /> <xs:element name="FirstName" type="xs:string" /> <xs:element name="Title" type="xs:string" minOccurs="0" /> <xs:element name="TitleOfCourtesy" type="xs:string" minOccurs="0" /> <xs:element name="BirthDate" type="xs:dateTime" minOccurs="0" /> <xs:element name="HireDate" type="xs:dateTime" minOccurs="0" /> <xs:element name="Address" type="xs:string" minOccurs="0" /> <xs:element name="City" type="xs:string" minOccurs="0" /> <xs:element name="Region" type="xs:string" minOccurs="0" /> <xs:element name="PostalCode" type="xs:string" minOccurs="0" /> <xs:element name="Country" type="xs:string" minOccurs="0" /> <xs:element name="HomePhone" type="xs:string" minOccurs="0" /> <xs:element name="Extension" type="xs:string" minOccurs="0" /> <xs:element name="Photo" type="xs:base64Binary" minOccurs="0" /> <xs:element name="Notes" type="xs:string" minOccurs="0" /> <xs:element name="ReportsTo" type="xs:int" minOccurs="0" /> <xs:element name="PhotoPath" type="xs:string" minOccurs="0" /> </xs:sequence> </xs:complexType> </xs:element> </xs:choice> </xs:complexType> <xs:unique name="DocumentKey1" msdata:PrimaryKey="true"> <xs:selector xpath=".//mstns:Employees" /> <xs:field xpath="mstns:EmployeeID" /> </xs:unique> </xs:element> </xs:schema> 
end example

Generating a Typed DataSet from a Schema

Now let's generate a typed DataSet from the schema you just added to the XML designer. The Generate Dataset option generates a typed DataSet for an XML schema. You can find the Generate Dataset option after right-clicking on the XML designer.

This action generates a DataSet class and adds it to your project. If you look in your Class View, you see the Document class derives from a DataSet and its members. The Document class looks like Figure 6-18 in the Class Wizard.


Figure 6-18: The DataSet-derived class in the Class View

Note

The Generate Dataset option may not generate a DataSet if an XML schema isn't designed properly.

Once you have a DataSet object, you can use it in any way you want.




Applied ADO. NET(c) Building Data-Driven Solutions
Applied ADO.NET: Building Data-Driven Solutions
ISBN: 1590590732
EAN: 2147483647
Year: 2006
Pages: 214

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