【发布时间】:2014-05-10 14:00:27
【问题描述】:
我有一个 XML 文件,其中包含这样的日期和时间
<date>3/24/2014</date>
<time>9:00 AM</time>
</playdate>
我将此数据存储在数据集中并希望添加到数据表中。我写这段代码是为了获取数据
DataRow dr = dt.NewRow();
dr["StartDate"] = ds.Tables[0].Rows[i][1].ToString();
dr["StartTime"] = ds.Tables[0].Rows[i][1].ToString();
但问题是当我在 excel 文件中导出数据时,我在同一列中的 excel 文件中同时获得了日期和时间。我想要 StartDate 中的日期和 StartTime 中的时间。如何拆分 XML 数据
编辑
string url = @"c:\temp\TestXML.xml";
StreamReader reader = new StreamReader(url);
string input = reader.ReadToEnd();
input = input.Trim();
input = input.Replace("- <", " <");
input = input.Replace(" & ", " & ");
input = input.Replace("W&W", "W&W");
input = input.Replace("", " ");
int userid = 0;
int j = 1;
try
{
DataSet ds = new DataSet();
ds.ReadXml(new StringReader(input));
// Creating Data Table according to Table Value Parameter in Database
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("CalenderName", typeof(string)));
dt.Columns.Add(new DataColumn("EventName", typeof(string)));
dt.Columns.Add(new DataColumn("StartDate", typeof(DateTime)));
dt.Columns.Add(new DataColumn("EndDate", typeof(DateTime)));
dt.Columns.Add(new DataColumn("StartTime", typeof(DateTime)));
dt.Columns.Add(new DataColumn("EndTime", typeof(DateTime)));
dt.Columns.Add(new DataColumn("Venue", typeof(string)));
dt.Columns.Add(new DataColumn("Address", typeof(string)));
dt.Columns.Add(new DataColumn("City", typeof(string)));
dt.Columns.Add(new DataColumn("State", typeof(string)));
dt.Columns.Add(new DataColumn("ZipCode", typeof(string)));
dt.Columns.Add(new DataColumn("PhoneNo", typeof(string)));
dt.Columns.Add(new DataColumn("Link", typeof(string)));
dt.Columns.Add(new DataColumn("Email", typeof(string)));
dt.Columns.Add(new DataColumn("Performer", typeof(string)));
dt.Columns.Add(new DataColumn("Tags", typeof(string)));
dt.Columns.Add(new DataColumn("Price", typeof(string)));
dt.Columns.Add(new DataColumn("PriceURL", typeof(string)));
dt.Columns.Add(new DataColumn("EventDetails", typeof(string)));
dt.Columns.Add(new DataColumn("TimeZoneName", typeof(string)));
dt.Columns.Add(new DataColumn("TimeZoneOffsetAtCreation", typeof(int)));
dt.Columns.Add(new DataColumn("TabIndex", typeof(int)));
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
DataRow dr = dt.NewRow();
string sDate = ds.Tables[2].Rows[i][0].ToString();
string sTime = ds.Tables[2].Rows[i][1].ToString();
DateTime dDateTime;
//cond: for checking the datetime parsing
if (DateTime.TryParseExact(sDate, "m/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dDateTime))
{
//sDate = dDateTime.ToString("M/dd/yyyy");
//dr["StartDate"] = sDate;
dr["StartDate"] = dDateTime;
}
//cond: for checking the datetime parsing
if (DateTime.TryParseExact(sTime, "hh:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out dDateTime))
{
//sTime = dDateTime.ToString("h:mm:ss");
//dr["StartTime"] = sTime;
dr["StartTime"] = dDateTime;
}
dr["EventName"] = ds.Tables[0].Rows[i][1].ToString();
//dr["StartDate"] = ds.Tables[2].Rows[i][0].ToString();
//dr["EndDate"] = ds.Tables[2].Rows[i][0].ToString();
//dr["StartTime"] = ds.Tables[2].Rows[i][1].ToString();
//dr["EndTime"] = ds.Tables[2].Rows[i][1].ToString();
dr["Venue"] = ds.Tables[1].Rows[i][1].ToString();
dr["Address"] = ds.Tables[1].Rows[i][2].ToString();
dr["City"] = ds.Tables[1].Rows[i][4].ToString();
dr["State"] = ds.Tables[1].Rows[i][5].ToString();
dr["ZipCode"] = Convert.ToInt32(ds.Tables[1].Rows[i][6].ToString());
dr["PriceURL"] = ds.Tables[0].Rows[i][2].ToString();
dr["EventDetails"] = ds.Tables[0].Rows[i][4].ToString();
dt.Rows.Add(dr);
j++;
}
gvXmlData.DataSource = dt;
gvXmlData.DataBind();
btnExportData.Visible = true;
【问题讨论】:
-
你能在excel文件中显示样本值吗?
-
2014 年 3 月 31 日上午 9:00:00 此数据显示在单个列中,而不是在单独的列中