【问题标题】:How to get DataSet.ReadXml to parse a DateTime attribute as a typed DateTime如何让 DataSet.ReadXml 将 DateTime 属性解析为类型化的 DateTime
【发布时间】:2010-02-09 12:51:00
【问题描述】:

我有一个 XML 字符串,其中包含格式为“dd/MM/yyyy hh:mm:ss”的日期。

我正在使用 DataSet.ReadXml() 将此 XML 加载到数据集中。

如何确保此日期作为类型化的 DateTime 存储在 DataSet 中,以便我可以对它进行相应的排序、格式化和行过滤。

我的测试工具如下:

ASPX 页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="XmlDateTypeList.aspx.cs" Inherits="Test.XmlDateTypeList" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Test</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server">
            <Columns>
                <asp:BoundField DataField="Name" HeaderText="Name" />
                <asp:BoundField DataField="Date" HeaderText="Date" DataFormatString="{0:dddd dd MMMM yyyy}" />
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

代码背后:

using System;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;

namespace Test
{
    public partial class XmlDateTypeList : 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 00:00:00")
                    )
                )
            );

            DataSet dataSet = new DataSet();
            dataSet.ReadXml(new StringReader(xDoc.ToString()));

            GridView1.DataSource = dataSet.Tables[0];
            GridView1.DataBind();
        }
    }
}

【问题讨论】:

    标签: c# asp.net xml datetime dataset


    【解决方案1】:

    这很痛苦,但根本问题是在这种情况下您的 xml 数据不能被强类型化,因为日期格式不是xs:date 格式,并且一旦数据在数据集。更复杂的事情是 .NET 不会自动将“28/01/2010 00:00:00”格式化为日期。因此,只需使用正确的数据类型从一个数据表复制到另一个数据表,并在此过程中重新格式化日期字符串。这段代码有效,但远非优雅。祝你好运。

        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();
            }
        }
    

    【讨论】:

    • XML不能强类型是什么意思?问题是您的 XML 数据不包含有效日期。 XML 中只有一种日期表示形式,但事实并非如此。如果您有兴趣让希望 XML 遵循 XML 标准的工具将您的字符串视为日期,请在 XML Schema 中查找 xs:Date 格式。
    • 好的,我会更新我的答案,您可以输入 xml 日期,但不能在示例中输入,因为日期格式很奇怪。这是一个有效日期(这是英国的标准),但不是每个 XML 模式的有效日期。
    • 感谢您的回答,对您有所帮助。最后,我实际上使用 xslt 中的字符串操作将日期转换为正确的格式,然后加载 xsd 以告诉数据集它是一个日期。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-19
    • 1970-01-01
    • 2020-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多