读XML文档动态构建DataTable列

  XmlDocument xml = new XmlDocument();
        xml.Load(Server.MapPath("load.xml"));
        XmlNodeList lst=xml.GetElementsByTagName("item");
        DataTable dt = new DataTable();
       
        foreach (XmlNode item1 in lst)
        {
            dt.Columns.Add(item1.Attributes["column"].Value.ToString(), Type.GetType(item1.Attributes["ctype"].Value.ToString()));
        }


 XML结构

<table>
<item column="LoginName" ctype="System.String"></item>
<item column="NickName" ctype="System.String"></item>
<item column="Mobile" ctype="System.String"></item>
</table>

 这样以后列名改变的时候就不用再修改cs文件而进行重新编译了!

相关文章:

  • 2021-06-17
  • 2021-12-29
  • 2022-03-03
  • 2021-07-22
猜你喜欢
  • 2021-12-29
  • 2021-10-19
  • 2022-12-23
  • 2021-04-24
  • 2022-12-23
  • 2021-10-17
相关资源
相似解决方案