怎样读取XML数据并绑定到DataGridView,该如何解决
展开全部
//判断是否有该文件
if (!File.Exists(@"C:\ZX_System\TimeManage\DN-TimeManage.xml"))
{
ltm = null;
}
else
{
//初始化 XML类
XmlDocument xml = new XmlDocument();
//找到路径
xml.Load(@"C:\ZX_System\TimeManage\DN-TimeManage.xml");
//找到主节点
XmlNode root = xml.SelectSingleNode("Root");
ltm = new List<TimeManage>();
//循环主节点
foreach (XmlNode item in root.ChildNodes)
{
TimeManage tm = new TimeManage();
//循环主节点的子节点
foreach (XmlNode sub in item.ChildNodes)
{
//判断赋值
switch (sub.Name)
{
case "OpenTime":
tm.OpenSourceTimeH = sub.InnerText;
break;
case "CloseTime":
tm.CloseSourceTimeH = sub.InnerText;
break;
case "TimeName":
tm.Timename = sub.InnerText;
break;
case "UserOnlyTime":
tm.UserText = Convert.ToDateTime(sub.InnerText);
break;
case "UserBeizhu":
tm.UserBeizhu = sub.InnerText;
break;
}
}
//保存集合
ltm.Add(tm);
}
}
自己最近写的一个,希望对你有用
if (!File.Exists(@"C:\ZX_System\TimeManage\DN-TimeManage.xml"))
{
ltm = null;
}
else
{
//初始化 XML类
XmlDocument xml = new XmlDocument();
//找到路径
xml.Load(@"C:\ZX_System\TimeManage\DN-TimeManage.xml");
//找到主节点
XmlNode root = xml.SelectSingleNode("Root");
ltm = new List<TimeManage>();
//循环主节点
foreach (XmlNode item in root.ChildNodes)
{
TimeManage tm = new TimeManage();
//循环主节点的子节点
foreach (XmlNode sub in item.ChildNodes)
{
//判断赋值
switch (sub.Name)
{
case "OpenTime":
tm.OpenSourceTimeH = sub.InnerText;
break;
case "CloseTime":
tm.CloseSourceTimeH = sub.InnerText;
break;
case "TimeName":
tm.Timename = sub.InnerText;
break;
case "UserOnlyTime":
tm.UserText = Convert.ToDateTime(sub.InnerText);
break;
case "UserBeizhu":
tm.UserBeizhu = sub.InnerText;
break;
}
}
//保存集合
ltm.Add(tm);
}
}
自己最近写的一个,希望对你有用
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询