Steve Wellens

Programming in the .Net environment



Create an XSD Schema….without knowing a darn thing about XSD.

Back in the old days, when dinosaurs roamed the earth, developers wanting to exchange data between applications used binary formatted data, hard-coded text field lengths, or delimited text files. Much parsing and error checking was involved. It was tedious.

With XML files a lot of that work can be done automatically… with one major drawback: You have to learn yet another 'language': XSD.

I don't know about you, but I feel the need to limit the amount of knowledge being poured into my brain on a daily basis… lest something gives (there is a fire-hose teacup analogy in there somewhere).

Here is how to create XML Schemas using classes in the .Net framework without knowing anything about XSD:

    protected void Button1_Click(object sender, EventArgs e)
        // The DataSet name becomes the root XML element
        DataSet MyDataSet = new DataSet("Golfers");
        // This can be confusing, the 'DataTable' will actually
        // become Elements (Rows) in the XML file.
        DataTable MyDataTable = new DataTable("Golfer");
        // Make columns attributes so we can 
        // link directly to a GridView
        MyDataTable.Columns.Add(new DataColumn("ID",
        MyDataTable.Columns.Add(new DataColumn("Name",
        MyDataTable.Columns.Add(new DataColumn("Birthday",
        // Write out the XSD
        // Put some data in the table
        DataRow TempRow;
        TempRow = MyDataTable.NewRow();
        TempRow["ID"] = 1;
        TempRow["Name"] = "Bobby Jones";
        TempRow["Birthday"] = new DateTime(1902, 3, 17);
        TempRow = MyDataTable.NewRow();
        TempRow["ID"] = 2;
        TempRow["Name"] = "Sam Snead";
        TempRow["Birthday"] = new DateTime(1912, 5, 27);
        TempRow = MyDataTable.NewRow();
        TempRow["ID"] = 3;
        TempRow["Name"] = "Tiger Woods";
        TempRow["Birthday"] = new DateTime(1975, 12, 30);
        // Write out the data

Here is how the data is saved, not bad eh?

  <?xml version="1.0" standalone="yes"?>
    <Golfer ID="1" Name="Bobby Jones" Birthday="1902-03-17T00:00:00-05:00" />
    <Golfer ID="2" Name="Sam Snead" Birthday="1912-05-27T00:00:00-05:00" />
    <Golfer ID="3" Name="Tiger Woods" Birthday="1975-12-30T00:00:00-06:00" />

Here is what the Schema looks like. Note, you can always go into the schema and tweak things.

  <?xml version="1.0" standalone="yes"?>
  <xs:schema id="Golfers" xmlns="" xmlns:xs="" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="Golfers" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="Golfer">
              <xs:attribute name="ID" type="xs:int" />
              <xs:attribute name="Name" type="xs:string" />
              <xs:attribute name="Birthday" type="xs:dateTime" />

Here is one way to validate the XML against the Schema:

    protected void Button2_Click(object sender, EventArgs e)
        // First, read in the XML schema
        DataSet MyDataSet = new DataSet();
        // Now, read in the XML file (it is validated 
        // against the schema when it is read in).
        // See how it looks in a GridView
        GridView1.DataSource = MyDataSet;


You can test the validation by modifying the XML file and trying to read it:

    <Golfer ID="abc" Name="Tiger Woods" Birthday="1975-12-30...

When the XML file is read, an exception will be generated. Without the XSD validation, the XML file is loaded without error.

I hope you find this useful.

Steve Wellens

By the way, Microsoft had an event for U of M graduates at Brit's Pub in Minneapolis, Minnesota last night. It was a great success. There was good food and great beer. In the drawing for prizes, the big ticket item, an Xbox 360, was won by the only female graduate in attendance…congratulations!

I got this pen :)

Posted: Jan 05 2009, 10:28 AM by SGWellens | with 7 comment(s) |
Filed under: , , , , ,


Sazzad said:

The easiest way i found to create a xsd is from visual studio. Just load your xml file and from the XML menu select Create Schema.. that will inspect your xml file and create the appropriate xml schema file for you. no programming needed.

# November 9, 2008 6:20 PM

SGWellens said:

Yes, thank you, I should have pointed that out.  The intent was to have XML/XSD be the transport mechanism between DataTables: DataTable->XML->DataTable.   Sometimes trying to create XML that maps to a DataTable, the way you want, takes some trial and error.  But if you already have an XML file, using the IDE is the way to go.

# November 9, 2008 7:32 PM

Hardy said:

opps!! Excelent men!!! Thanks very Much!!!!

# January 8, 2009 10:32 AM

Brian said:

I've been to Brits Pub - its a fun place.

# February 12, 2010 6:06 PM

niks said:

that was really good artical...its solved my problem and given me gr8 undestanding...thanx a lot..

I want to add new node can u help with that?

# February 24, 2010 2:14 AM

Brett said:

Love your sense of humor!!!

# February 19, 2011 10:45 AM

Wasiq said:

Very useful. xectly what i was looking for.

Thanks for this wonderful peace of work.

# June 26, 2011 6:20 AM

hukum said:


it's really work i really appriciate u....

# July 14, 2011 12:15 AM