欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ASP.NET将XML数据转换成DataTable

程序员文章站 2022-07-16 18:23:46
...
/// <summary>
        /// 获取部门列表
        /// </summary>
        /// <returns></returns>
        public DataTable getRTXDeptmentList()
        {
            util.RTXAction rtx = new util.RTXAction();
            string xmlstr = rtx.GetDepartmentlist();

            //GetDepartmentlist()返回XML

            //<Departments>

            //<Department Name = "测试部门1"/>

            //<Department Name = "测试部门2"/>

            //</Departments>

            XmlDocument xml = new XmlDocument();
            xml.LoadXml(xmlstr);

            DataTable dt = new DataTable();
            DataColumn dc = new DataColumn("deptname");
            dt.Columns.Add(dc);
            for (int i = 0; i < xml.ChildNodes[0].ChildNodes.Count; i++)
            {
                DataRow dr = dt.NewRow();
                dr["deptname"] = xml.ChildNodes[0].ChildNodes[i].Attributes["Name"].Value;
                dt.Rows.Add(dr);
            }
            return dt;
        }
相关标签: datatable xml