Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 610599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:41:34+00:00 2026-05-13T17:41:34+00:00

I have an XML string which contains Dates formatted dd/MM/yyyy hh:mm:ss. I’m loading this

  • 0

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 can I ensure that this Date is stored in the DataSet as a typed DateTime so that I can Sort, Format and RowFilter on it accordingly.

My test harness is as below:

ASPX Page:

<%@ 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>

Code Behind:

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();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T17:41:34+00:00Added an answer on May 13, 2026 at 5:41 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.