Custom DateTime formats when using DataSet.WriteXml in .NET

后端 未结 5 873
别跟我提以往
别跟我提以往 2020-12-06 13:03

I\'ve got an issue where I am writing a DataSet to XML that has a column of type DateTime and I want to control the output format.

DataSet data = LoadDataSet         


        
相关标签:
5条回答
  • 2020-12-06 13:37

    There is one standard format for DateTime in XML. That's the format that WriteXml will use. If you need a different format, then you need to not use DateTime. As others have said, use String instead.

    0 讨论(0)
  • 2020-12-06 13:42

    If the file is regular, and the exported XML seems so, you can just rewrite it using simple loop. Easy and quick solution. Use some of my code if you want:

    private void rewriteXML(string oldFile, string newFile, int startPos, int strLength)
            {
                try
                {
                    File.Delete(newFile);
                }
                catch { Console.WriteLine("File didn't existed."); }
                StreamReader SR = new StreamReader(oldFile);
                string data;
                StreamWriter SW = new StreamWriter(newFile);
                while ((data = SR.ReadLine()) != null)
                {
                    if (data.Contains("<start>"))
                    {
                        string ln_tmp = data.Replace(" ", "");
                        string newline = "    <start>" + ln_tmp.Substring(startPos, strLength) + "</start>";
                        SW.WriteLine(newline);
                    }
                    else
                        SW.WriteLine(data);
                }
                SR.Close();
                SW.Close();
            }
    

    The output is like:

    <start>19:34</start>
    

    Instead of:

    <start>2013-03-17T19:34:00+01:00</start>
    
    0 讨论(0)
  • 2020-12-06 13:44

    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.

    0 讨论(0)
  • 2020-12-06 13:46

    Apply XSLT transformation to DataSet: (from MSDN) (I believe you will find XSLT example to convert DateTime format, or see post on SO regarding XSLT and DateTIme formats)

    DataSet custDS = new DataSet("CustomerDataSet");
    XmlDataDocument xmlDoc = new XmlDataDocument(custDS); 
    
    XslTransform xslTran = new XslTransform();
    xslTran.Load("transform.xsl");
    
    XmlTextWriter writer = new XmlTextWriter("xslt_output.html", 
      System.Text.Encoding.UTF8);
    
    xslTran.Transform(xmlDoc, null, writer);
    writer.Close();
    
    0 讨论(0)
  • 2020-12-06 13:57

    Dirty but simple solution to convert datatime to string :

    select concat(datetime_field,'') datetime_field from table_name
    
    0 讨论(0)
提交回复
热议问题