Generating XSDs
In our previous article we saw how we can use
XSDs in conjunction with SQLXML. But how can we generate XSDs rather than
typing them separately. Here are some simple steps to achieve the same using VS
.NET IDE.
Step 1:
Open an Visual Studio .NET project, and on the Project
menu, click Add New Item.
Step 2:
In the Add New Item dialog, in the templates pane,
select XML Schema and Open.
Step 3:
From the View menu, click Server Explorer (or
press CTRL+ALT+S) to open Server Explorer.
Step 4:
Expand Servers, Select <Machine Name>, SQL
Servers, Select <Machine Name>, Select Pubs Database and Tables.
Step 5:
Drag and drop the Orders table on to the design
surface.
Step 6:
We are done in generating the XSD. Now move to the XML
tab. You can see an ouput as outlined.
<?xml version="1.0"
encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:sql="urn:schemas-microsoft-com:mapping-schema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Document"> <-
<xs:complexType>
<-
<xs:choice
maxOccurs="unbounded"> <-
<xs:element name="authors">
<xs:complexType>
<xs:sequence>
<xs:element name="au_id" type="xs:string" />
<xs:element name="au_lname" type="xs:string" />
<xs:element name="au_fname" type="xs:string" />
<xs:element name="phone" type="xs:string" />
<xs:element name="address" type="xs:string" minOccurs="0" />
<xs:element name="city" type="xs:string" minOccurs="0" />
<xs:element name="state" type="xs:string" minOccurs="0" />
<xs:element name="zip" type="xs:string" minOccurs="0" />
<xs:element name="contract" type="xs:boolean" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice> <-
</xs:complexType> <-
<xs:unique name="DocumentKey1"
msdata:PrimaryKey="true"> <-
<xs:selector xpath=".//authors"
/> <-
<xs:field xpath="au_id" />
<-
</xs:unique> <-
</xs:element> <-
</xs:schema>
Step 7:
Now we cannot use this XSD generated directly. We have
to have a couple of modifications and have to remove some content to get the
final version. I've removed some parts of the code and you can view the final
document version:
<?xml version="1.0"
encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:sql="urn:schemas-microsoft-com:mapping-schema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="authors">
<xs:complexType>
<xs:sequence>
<xs:element name="au_id" type="xs:string" />
<xs:element name="au_lname" type="xs:string" />
<xs:element name="au_fname" type="xs:string" />
<xs:element name="phone" type="xs:string" />
<xs:element name="address" type="xs:string" minOccurs="0" />
<xs:element name="city" type="xs:string" minOccurs="0" />
<xs:element name="state" type="xs:string" minOccurs="0" />
<xs:element name="zip" type="xs:string" minOccurs="0" />
<xs:element name="contract" type="xs:boolean" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
And I think the deletion is easy to do. You can just
see a note (<-) that tells
which line to delete. As simple as that.
Using this XML you can use them in your code as shown
in one of my earlier article in "
Using XSD".
|