Custom DateTime formats when using DataSet.WriteXml in .NET

This workaround might suit if you can live with just the timezone info getting stripped out. The default for the DateTimeMode property of DateTime columns in DataSets is UnspecifiedLocal. You could explicitly set the DateTimeMode to Unspecified, which means that the timezone part does not get serialized.

e.g.

You can use a function like this :

    public static void RemoveTimezoneForDataSet(DataSet ds)
    {
        foreach (DataTable dt in ds.Tables)
        {
            foreach (DataColumn dc in dt.Columns)
            {

                if (dc.DataType == typeof(DateTime))
                {
                    dc.DateTimeMode = DataSetDateTime.Unspecified;
                }
            }
        }
    }

Example of usage :

    DataSet ds = new DataSet();
    DataTable dt = new DataTable("t1");
    dt.Columns.Add("ID", typeof(int));
    dt.Columns.Add("DT", typeof(DateTime));
    dt.Rows.Add(new object[] { 1, new DateTime(2009, 1, 1) });
    dt.Rows.Add(new object[] { 2, new DateTime(2010, 12, 23) });

    ds.Tables.Add(dt);

    ds.WriteXml("c:\\Standard.xml");

    RemoveTimezoneForDataSet(ds);

    ds.WriteXml("c:\\WithoutTimezone.xml");

Output :

Standard.xml :

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <t1>
    <ID>1</ID>
    <DT>2009-01-01T00:00:00+11:00</DT>
  </t1>
  <t1>
    <ID>2</ID>
    <DT>2010-12-23T00:00:00+11:00</DT>
  </t1>
</NewDataSet>

WithoutTimezone.xml :

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <t1>
    <ID>1</ID>
    <DT>2009-01-01T00:00:00</DT>
  </t1>
  <t1>
    <ID>2</ID>
    <DT>2010-12-23T00:00:00</DT>
  </t1>
</NewDataSet>

If you don't like the idea of modifying the DataColumns of your original DataSet, you could make a copy of it, then call the function on the copy.