subscribe: Posts | Comments

Creating an XSD Schema using VS.Net Tools

0 comments



An Xsd is basically a Class that defines what your xml will
eventually look like (not including data)

1)Open Visual Studio.net and open a new windows application project. Lets call
it XMLEmployees

2)Now in the solution explorer add a new item – XML Schema. Name this Schema
EmployeeListSchema.xsd

3)From the toolbox drag a complex type onton the design surface. Rename the
complextype1 to Employer.

4)In the row below this add in 2 elements called employer_id which is of type
long and employer_name which is a string.

5) Next drag an attribute onto the design surface. Name this attribute Country
and make sure it is a string.

6) Now drag the attribute and hover over the Employer complex type and then drop
it onto it. Try and get the attribute to be listed underneath the 2 elements.

You should have something like this at this point:

Employer

E employer_id long

E employer_name string

A country string

In this instance Country is an attribute of Employer where as employer_id and
employer_name are sub elements of Employer.

7) Now drag and drop an attributegroup onto the design surface from the toolbox
– call it EmployerAttributes.

8) Add some attributes to the attributegroup such as CompanyName of type string
and CompnayFormedDate of type Date, and wages of type decimal.

9) Now add a complex type from the toolbox and call it Job. Add some elements to
this, add a job_id element as a long, employment_id element as a long, jobtitle
element as type string. Lastly add an attribute groupThis will create a link to
a blank attributegroup – if you call it the same name as the previous
attributegroup it will create a copy of the previous attributegroup. (EmployerAttributes).
This will add a reference=attributegroup to this attribute group from the
complex type.

We can now use this attributegroup again and again by simply adding a reference
just like the way I just did.

10) Now right click on the deigner and select auto-arrange and it will be
displayed neatly.

11) Again add a complex type and call it Contact. Add elements named Address of
type string, city of type string, state of type string, and lastly zip.

With zip were going to make it have some validation.

12) Drag a simpleType onto the designsurface and name it State of type string.
Now we add some properties to the simpleType – select the row below and from the
drop down choose length and enter a vlaue of 2 acroos from the length property.

13) Next drag a facet object from the toolbox into the simpleType you just
created and make the property enumeration and the value as IL (for the state).
Create some more facet’s by either dragiing the facet object onto the simpletype
or right click the simpletype dropown where the types are and choose facet (an F
will be visible for facet). Make sure you have say 3 facets (IL,CA and TX
perhaps).

14) Now go back to the complex type for COntact and across from State where we
previously had string, select the dropdown and choose State which refers to our
simpleType just created.

15) Add 1 more complex type called Author, again add elements such as author_id
as of type long and author_name as of type string, contact_info but this time of
type contact. Ths will add a link in the deigner to our Contact complex type.

16) Now add an element and call it Author and choose a type next to the name and
select the type called author.This will create an instance of the author which
you wont be able to change as this is defined elsewhere.

17) Lastly look at the xml view in the designer and you will notice the xml
which has been built up from this exercise.