error in dataset.ReadXml()?

  • Thread starter Thread starter Stephen Alpert
  • Start date Start date
S

Stephen Alpert

Attached is simple code to populate a dataset from xml:

ds = new DataSet("DateTest");
DataTable dt = new DataTable("Person");
DataColumn dc = new
DataColumn("Name",Type.GetType("System.String"));
dt.Columns.Add(dc);
DataColumn dcd = new
DataColumn("DOB",Type.GetType("System.DateTime"));
dt.Columns.Add(dcd);
ds.Tables.Add(dt);
SchemaText.Text = ds.GetXmlSchema();

// load it up
String data="<?xml version='1.0'?><Club>" +
"<Person><Name>Daffy Duck</Name><DOB>1/2/1952</DOB></Person>" +
"<Person><Name>Fred Flints</Name><DOB>5/3/1942</DOB></Person>" +
"</Club>";

try
{
ds.ReadXml( new StringReader(data), XmlReadMode.InferSchema );
}
catch (Exception ex)
{
Msg.Text = ex.Message;
dt.Rows.Add( new Object[] { "Daffy Duck", "1/2/1952" } );
dt.Rows.Add( new Object[] { "Fred Flints", "5/3/1942" } );
}
DataText.Text = ds.GetXml();

When this is run, the schema looks like:

<?xml version="1.0" encoding="utf-16"?>
<xs:schema id="DateTest" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="DateTest" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Person">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="xs:string" minOccurs="0" />
<xs:element name="DOB" type="xs:dateTime" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>

Notice the dateTime type for DOB

When I try to ReadXML into the dataset (see code above), I get an exception

String was not recognized as a valid DateTime.

But I can add rows to the table manually (see code above), and get:

<Club>
<Person>
<Name>Daffy Duck</Name>
<DOB>1952-01-02T00:00:00.0000000-05:00</DOB>
</Person>
<Person>
<Name>Fred Flints</Name>
<DOB>1942-05-03T00:00:00.0000000-04:00</DOB>
</Person>
</Club>

I'm guessing from looking at the stack that ReadXml calls DateTime.ParseExact
and the Rows.Add calls DateTime.Parse!

HELP!

tnx,
steve
my email (e-mail address removed) is encrypted with ROT13 (www.rot13.org)
 
Back
Top