Custom DateTime formats when using DataSet.WriteXml in .NET

后端 未结 5 879
别跟我提以往
别跟我提以往 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: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(""))
                    {
                        string ln_tmp = data.Replace(" ", "");
                        string newline = "    " + ln_tmp.Substring(startPos, strLength) + "";
                        SW.WriteLine(newline);
                    }
                    else
                        SW.WriteLine(data);
                }
                SR.Close();
                SW.Close();
            }
    

    The output is like:

    19:34
    

    Instead of:

    2013-03-17T19:34:00+01:00
    

提交回复
热议问题