Loading the Schema

Listing 2. Loading an XSD schema into a DataSet causes DataColumn values to conform to a specific data type. The code attempts to insert a new row into the Orders DataTable (table 1 in the DataSet). An error occurs because the value inserted into the ShippedDate DataColumn does not conform to a valid DateTime (see Listing 3).

private void Page_Load(
   object sender, System.EventArgs e) {
   DataSet ds = new DataSet();
   ds.ReadXmlSchema(Server.MapPath(
      "XML/CustomersOrders.xsd"));
   ds.ReadXml(Server.MapPath(
      "XML/CustomersOrders.xml"));
   try {
            /*
            Test out the schema by inserting bad data
            This will cause an error to occur since the data
            must be a valid DateTime 
            */
      DataRow row = ds.Tables[1].NewRow();
      row["ShippedDate"] = "12321";
      ds.Tables["Orders"].Rows.Add(row);
   }
   catch (Exception exp) {
      Response.Write(exp.Message);
   }
   DG.DataSource = ds.Tables[1].DefaultView;
   DG.DataBind();
}