Click here to Skip to main content
15,881,559 members
Articles / Programming Languages / XML

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

Rate me:
Please Sign up or sign in to vote.
4.91/5 (3 votes)
8 Apr 2009CPOL1 min read 15.4K   12  
Back in the old days, when dinosaurs roamed the earth, developers wanting to exchange data between applications used binary formatted data, hardcoded text field lengths, or delimited text files. Much parsing and error checking was involved. It was tedious.

Back in the old days, when dinosaurs roamed the earth, developers wanting to exchange data between applications used binary formatted data, hardcoded 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:

C#
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");

    MyDataSet.Tables.Add(MyDataTable);

    // Make columns attributes so we can 
    // link directly to a GridView
    MyDataTable.Columns.Add(new DataColumn("ID",
                                           typeof(System.Int32),
                                           null,
                                           MappingType.Attribute));

    MyDataTable.Columns.Add(new DataColumn("Name",
                                           typeof(String),
                                           null,
                                           MappingType.Attribute));

    MyDataTable.Columns.Add(new DataColumn("Birthday",
                                           typeof(DateTime),
                                           null,
                                           MappingType.Attribute));

    // Write out the XSD
    MyDataSet.WriteXmlSchema(@"C:\GolfersSchema.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);
    MyDataTable.Rows.Add(TempRow);

    TempRow = MyDataTable.NewRow();
    TempRow["ID"] = 2;
    TempRow["Name"] = "Sam Snead";
    TempRow["Birthday"] = new DateTime(1912, 5, 27);
    MyDataTable.Rows.Add(TempRow);

    TempRow = MyDataTable.NewRow();
    TempRow["ID"] = 3;
    TempRow["Name"] = "Tiger Woods";
    TempRow["Birthday"] = new DateTime(1975, 12, 30);
    MyDataTable.Rows.Add(TempRow);

    // Write out the data

    MyDataSet.WriteXml(@"C:\Golfers.xml");
}

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

XML
<?xml version="1.0" standalone="yes"?>
  <Golfers>
    <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" />
  </Golfers>

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

XML
  <?xml version="1.0" standalone="yes"?>
  <xs:schema id="Golfers" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="Golfers" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="Golfer">
            <xs:complexType>
              <xs:attribute name="ID" type="xs:int" />
              <xs:attribute name="Name" type="xs:string" />
              <xs:attribute name="Birthday" type="xs:dateTime" />
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema><!--EndFragment-->

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

C#
protected void Button2_Click(object sender, EventArgs e)
{
    // First, read in the XML schema
    DataSet MyDataSet = new DataSet();
    MyDataSet.ReadXmlSchema(@"C:\GolfersSchema.xsd");

    // Now, read in the XML file (it is validated
    // against the schema when it is read in).
    MyDataSet.ReadXml(@"C:\Golfers.xml");

    // See how it looks in a GridView
    GridView1.DataSource = MyDataSet;
    GridView1.DataBind();
}

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

C#
<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

This article was originally posted at http://weblogs.asp.net/stevewellens/privaterss.aspx

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
EndWell Software, Inc.
United States United States
I am an independent contractor/consultant working in the Twin Cities area in Minnesota. I work in .Net, Asp.Net, C#, C++, XML, SQL, Windows Forms, HTML, CSS, etc., etc., etc.

Comments and Discussions

 
-- There are no messages in this forum --