.NET解析xml字符串,通过反射给实体类对象赋值,获取实体类数据列表

/// <summary>
        /// 解析xml字符串 转换为实体类列表数据
        /// </summary>
        /// <param name="xmlStr">xml字符串</param>
        /// <returns></returns>
        /// xml字符串格式如下:(Item的节点客户有多对,每对代表一条数据)
        /// <?xml version="1.0" encoding="utf-8" ?><BOSS><Item>
        /// <UserId>用户编号</UserId>
        /// <Type>用户类型</Type>
        /// <Group>用户群</Group>
        /// <State>用户状态</State>
        /// <SIM>SIM卡号</SIM>
        /// <STB>机顶盒号</STB>
        /// <Memo>备注</Memo>
        /// <Crtime>创建日期</Crtime>
        /// <CustomerId>客户编号</CustomerId>
        /// <CustomerName>客户名称</CustomerName>
        /// <Address>用户地址</Address>
        /// <Phone>电话</Phone>
        /// <Mobile>手机</Mobile>
        /// </Item><Code></Code><Msg></Msg></BOSS>
        public List<UserInfo> GetUserList(string xmlStr)
        {
            System.Type t = typeof(UserInfo);
            object obj = Activator.CreateInstance(t, null);//创建指定类型实例
            PropertyInfo[] fields = obj.GetType().GetProperties();//获取指定对象的所有公共属性

            List<UserInfo> ulist = new List<UserInfo>();
            XmlDocument xml = new XmlDocument();
            xml.LoadXml(xmlStr);//加载xml
            XmlNodeList xlist = xml.GetElementsByTagName("Item");//获取Item节点列表
            foreach (XmlNode xn in xlist)//遍历Item节点下的子节点
            {

                object u = Activator.CreateInstance(t, null);//创建指定类型实例

                foreach (PropertyInfo p in fields)
                {
                    for (int i = 0; i < xn.ChildNodes.Count; )
                    {
                        if (xn.ChildNodes[i].Name == p.Name)
                        {
                            p.SetValue(u, xn.ChildNodes[i].InnerText, null);//给创建的实例属性赋值
                        }
                        i++;
                    }
                }
                ulist.Add((UserInfo)u);
            }
            return ulist;
        }

或者将该方法写为一个通用的方法进行调用

/// <summary>
        /// 获取一个实体类的object数据列表
        /// </summary>
        /// <param name="xmlStr">指定格式的xml字符串</param>
        /// <param name="t">实体类的发射类对象</param>
        /// <returns>实体类的object数据类别</returns>
        /// 不可直接将数据类别转换为实体类的真正类型数据列表,转换时,需遍历没有object逐个转换为实体类型,然后附加到List
        public List<object> GetObjectList(string xmlStr, Type t)
        {
            List<object> olist = new List<object>();
            object obj = Activator.CreateInstance(t, null);//创建指定类型实例
            PropertyInfo[] fields = obj.GetType().GetProperties();//获取指定对象的所有公共属性

            XmlDocument xml = new XmlDocument();
            xml.LoadXml(xmlStr);//加载xml
            XmlNodeList xlist = xml.GetElementsByTagName("Item");//获取Item节点列表
            foreach (XmlNode xn in xlist)//遍历Item节点下的子节点
            {

                object u = Activator.CreateInstance(t, null);//创建指定类型实例

                foreach (PropertyInfo p in fields)
                {
                    for (int i = 0; i < xn.ChildNodes.Count; )
                    {
                        if (xn.ChildNodes[i].Name == p.Name)
                        {
                            p.SetValue(u, xn.ChildNodes[i].InnerText, null);//给创建的实例属性赋值
                        }
                        i++;
                    }
                }
                olist.Add(u);
            }
            return olist;
        }

string xmlStr="指定格式的xml字符串";

System.Type t=typeof(实体类);

List<object> olist=GetObjectList(xmlStr,t);

List<实体类> list=new List<实体类>

然而此处存在一个问题,就是获取的只是object类的数据列表,并且List<object> 是无法转换为 List<实体类> 的,这时候我们可以通过遍历的形式进行转换

foreach(object o in olist)

{

  list.Add((实体类)o);

}

原文地址:https://www.cnblogs.com/Shang0109/p/3180026.html