我有一个包含日期和版本的XML文件。像这样的时间
<date>3/24/2014</date>
<time>9:00 AM</time>
</playdate>
我将这些数据存储在数据集&amp;想要添加数据表。我写这个代码来获取数据
DataRow dr = dt.NewRow();
dr["StartDate"] = ds.Tables[0].Rows[i][1].ToString();
dr["StartTime"] = ds.Tables[0].Rows[i][1].ToString();
但问题是当我在excel文件中导出数据时我得到了date&amp;在同一列的excel文件中的时间。我想在我的StartDate&amp;在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;
答案 0 :(得分:0)
您在 StartDate 和 StartTime 中访问相同的单元格值。 很可能下一个细胞应该给你时间价值。
您也可以格式化与XML中指定相同的日期时间,例如(MM / DD / YYYY) 时间小时分和秒也是如此。
请参阅此处的代码示例:
string sDate = ds.Tables[0].Rows[i][0].ToString();
string sTime = ds.Tables[0].Rows[i][1].ToString();
DateTime dDateTime;
DataRow dr = dt.NewRow();
//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;
}
答案 1 :(得分:0)
尝试使用DateTime.ParseExact
string cellValue = "3/31/2014 9:00:00 AM";// this can be a cell value from excel file like ds.Tables[0].Rows[i][1].ToString()
DateTime date = DateTime.ParseExact(
cellValue,
"M/dd/yyyy h:mm:ss tt",
CultureInfo.InvariantCulture);
string startDate = date.ToString("M/dd/yyyy");
string startTime = date.ToString("h:mm:ss tt");