How to get DataSet.ReadXml to parse a DateTime attribute as a typed DateTime

后端 未结 1 1282
别那么骄傲
别那么骄傲 2021-01-15 20:55

I have an XML string which contains Dates formatted \"dd/MM/yyyy hh:mm:ss\".

I\'m loading this XML into a dataset using DataSet.ReadXml().

How c

相关标签:
1条回答
  • 2021-01-15 21:22

    This is painful but the root issue is that your xml data can't be strongly typed in this case because the date format is not in the xs:date format and you can't change the type once the data is in the data set. Further complicating things is that .NET wont auto format "28/01/2010 00:00:00" as a date. So, just a copy from one data table to another with the correct data type and reformatting the date string along the way. This code works, but is far from elegant. Good Luck.

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Web.UI;
        using System.Web.UI.WebControls;
        using System.Xml.Linq;
        using System.Data;
        using System.IO;
    
        public partial class xmltest : System.Web.UI.Page
        {
    
            protected void Page_Load(object sender, EventArgs e)
            {
                XDocument xDoc = new XDocument(
                    new XElement("List",
                        new XElement("Item",
                            new XAttribute("Name", "A"),
                            new XAttribute("Date", "21/01/2010 00:00:00")
                        ),
                        new XElement("Item",
                            new XAttribute("Name", "B"),
                            new XAttribute("Date", "12/01/2010 00:00:00")
                        ),
                        new XElement("Item",
                            new XAttribute("Name", "C"),
                            new XAttribute("Date", "10/01/2010 00:00:00")
                        ),
                        new XElement("Item",
                            new XAttribute("Name", "D"),
                            new XAttribute("Date", "28/01/2010 12:33:22")
                        )
                    )
                );
    
                DataSet dataSet = new DataSet();
                dataSet.ReadXml(new StringReader(xDoc.ToString()));
    
                DataSet dataSet2 = dataSet.Clone();
    
                dataSet2.Tables[0].Columns[1].DataType = typeof(DateTime);
    
                // painful, painful copy over code from dataset1 to dataset2 
                foreach (DataRow r in dataSet.Tables[0].Rows)
                {
    
                    DataRow newRow = dataSet2.Tables[0].NewRow();
                    newRow["name"] = r["name"];
    
                    newRow["Date"] = DateTime.ParseExact(r["Date"].ToString(), "dd/MM/yyyy HH:mm:ss", CultureInfo.CurrentCulture);
    
                    dataSet2.Tables[0].Rows.Add(newRow);
    
                }
    
                GridView1.DataSource = dataSet2.Tables[0];
                GridView1.DataBind();
            }
        }
    
    0 讨论(0)
提交回复
热议问题