Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Columns types got lost using Dataset.WriteXML

The problem is easy to describe.

1/ I have a dataset containing a datatable with 4 columns from different type: int32, string, datetime, bool. I populate the datatable with one row.
2/ I save the dataset data and schema to a XML file using DataSet.WriteXml and the XmlWriteMode WriteSchema parameter.
3/ I create a new empty dataset and I fill it with the XML file using DataSet.ReadXml and then XmlReadMode.InferSchema parameter.
Surprise ! The datatable is created and populated with one record but all columns have the string type.

What went wrong ?
Thank you in advance.
Emmanuel Fayet

>>
System.Data.DataSet oDataSet;
System.Data.DataSet oDataSet2;
System.Data.DataTable oDataTable;
System.Data.DataRow oDataRow;

//--Step1
oDataSet = new System.Data.DataSet( "datasetdummy" );
oDataTable = oDataSet.Tables.Add("dummy");

oDataTable.Columns.Add(new System.Data.DataColumn("IntegerValue", typeof(Int32)));
oDataTable.Columns.Add(new System.Data.DataColumn("StringValue", typeof(string)));
oDataTable.Columns.Add(new System.Data.DataColumn("DateTimeValue", typeof(DateTime)));
oDataTable.Columns.Add(new System.Data.DataColumn("BoolValue", typeof(bool)));

oDataRow = oDataTable.NewRow();
oDataRow["IntegerValue"] = 1;
oDataRow["StringValue"] = "This is a string";
oDataRow["DateTimeValue"] = System.DateTime.Now;
oDataRow["BoolValue"] = true;
oDataTable.Rows.Add(oDataRow);
inspect_table( oDataSet.Tables["dummy"] );

//--Step2
oDataSet.WriteXml( "C:\\home\\source\\vs2003\\bench\\datasetdummy.xml" , System.Data.XmlWriteMode.WriteSchema );

//--Step3
oDataSet2 = new System.Data.DataSet( "datasetdummy" );
oDataSet2.ReadXml( "C:\\home\\source\\vs2003\\bench\\datasetdummy.xml" , System.Data.XmlReadMode.InferSchema );
inspect_table( oDataSet2.Tables["dummy"] );

with
private void inspect_table(System.Data.DataTable oTable)
{
trace( "Inspect table:" + oTable.ToString() );
foreach (System.Data.DataColumn oColumn in oTable.Columns)
{
trace( oColumn.DataType.ToString() + ": " + oColumn.ToString() );
}
trace("");
}
>>

Emmanuel Fayet
Wednesday, January 21, 2004

What does the XML file look like?

Greg Hurlman
Wednesday, January 21, 2004

Pretty nice...

>>
<?xml version="1.0" standalone="yes"?>
<datasetdummy>
  <xs:schema id="datasetdummy" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="datasetdummy" msdata:IsDataSet="true" msdata:Locale="fr-FR">
      <xs:complexType>
        <xs:choice maxOccurs="unbounded">
          <xs:element name="dummy">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="IntegerValue" type="xs:int" minOccurs="0" />
                <xs:element name="StringValue" type="xs:string" minOccurs="0" />
                <xs:element name="DateTimeValue" type="xs:dateTime" minOccurs="0" />
                <xs:element name="BoolValue" type="xs:boolean" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <dummy>
    <IntegerValue>1</IntegerValue>
    <StringValue>This is a string</StringValue>
    <DateTimeValue>2004-01-22T10:18:35.7713225+01:00</DateTimeValue>
    <BoolValue>true</BoolValue>
  </dummy>
</datasetdummy>
>>

Emmanuel Fayet
Thursday, January 22, 2004

Problem fixed by storing the schema in a external file XSD file and then by loading the schema first and then data 

To summarize, Dataset.ReadXML with the InferSchema parameter does not work as expected...

>>
oDataSet.WriteXmlSchema( "C:\\home\\source\\vs2003\\bench\\datasetdummy.xsd" );
oDataSet.WriteXml( "C:\\home\\source\\vs2003\\bench\\datasetdummy.xml" , System.Data.XmlWriteMode.IgnoreSchema );

oDataSet2.ReadXmlSchema( "C:\\home\\source\\vs2003\\bench\\datasetdummy.xsd" );
oDataSet2.ReadXml( "C:\\home\\source\\vs2003\\bench\\datasetdummy.xml" );
>>

Emmanuel Fayet
Thursday, January 22, 2004

"To summarize, Dataset.ReadXML with the InferSchema parameter does not work as expected..."

No -- the problem is: you don't read the docs!!! --> "InferSchema: Ignores any inline schema, infers schema from the data and loads the data"

You should have used XmlReadMode.ReadSchema (or Auto).

Duncan Smart
Saturday, January 24, 2004

*  Recent Topics

*  Fog Creek Home