怎样读取XML数据并绑定到DataGridView,该如何解决

2024-12-24 12:08:10
推荐回答(1个)
回答1:

//判断是否有该文件
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();
//循环主节点
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);
}
}
自己最近写的一个,希望对你有用