博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
实体类与xml互相转换通用 .
阅读量:6847 次
发布时间:2019-06-26

本文共 1923 字,大约阅读时间需要 6 分钟。

// <summary>

        ///  实体类序列化成xml

        /// </summary>
        /// <param name="enitities">实体.</param>
        /// <param name="headtag">节点名称</param>
        /// <returns></returns>
        public static string ObjListToXml<T>(List<T> enitities, string headtag)
        {
            StringBuilder sb = new StringBuilder();
            PropertyInfo[] propinfos = null;
            sb.AppendLine("<?xml version=/"1.0/" encoding=/"utf-8/"?>");
            sb.AppendLine("<"+headtag+">");
            foreach (T obj in enitities)
            {
                //初始化propertyinfo
                if (propinfos == null)
                {
                    Type objtype = obj.GetType();
                    propinfos = objtype.GetProperties();
                }

                sb.AppendLine("<item>");
                foreach (PropertyInfo propinfo in propinfos)
                {
                    sb.Append("<");
                    sb.Append(propinfo.Name);
                    sb.Append(">");
                    sb.Append(propinfo.GetValue(obj, null));
                    sb.Append("</");
                    sb.Append(propinfo.Name);
                    sb.AppendLine(">");
                }
                sb.AppendLine("</item>");
            }
            sb.AppendLine("</" + headtag + ">");
            return sb.ToString();
        }

xml转行为实体类

 /// <summary>

        /// xml文件转化为实体类列表
        /// </summary>
        /// <typeparam name="T">实体名称</typeparam>
        /// <param name="xml">您的xml文件</param>
        /// <param name="headtag">xml头文件</param>
        /// <returns>实体列表</returns>
        public static List<T> XmlToObjList<T>(string xml, string headtag)
            where T : new()
        {

            List<T> list = new List<T>();

            XmlDocument doc = new XmlDocument();
            PropertyInfo[] propinfos = null;
            doc.LoadXml(xml);
            //XmlNodeList nodelist = doc.SelectNodes(headtag);
            XmlNodeList nodelist = doc.GetElementsByTagName(headtag);
            foreach (XmlNode node in nodelist)
            {
                T entity = new T();
                //初始化propertyinfo
                if (propinfos == null)
                {
                    Type objtype = entity.GetType();
                    propinfos = objtype.GetProperties();
                }
                //填充entity类的属性
                foreach (PropertyInfo propinfo in propinfos)
                {
                    //实体类字段首字母变成小写的
                    string name = propinfo.Name.Substring(0, 1) + propinfo.Name.Substring(1, propinfo.Name.Length - 1);
                    XmlNode cnode = node.SelectSingleNode(name);
                    string v = cnode.InnerText;
                    if (v != null)
                        propinfo.SetValue(entity, Convert.ChangeType(v, propinfo.PropertyType), null);
                }
                list.Add(entity);

            }

            return list;

        }

转载地址:http://igoul.baihongyu.com/

你可能感兴趣的文章
安装包制作工具 SetupFactory使用2 API清单
查看>>
js中with、this的用法
查看>>
SharePoint 2013之Office Web Apps Server(1)
查看>>
Android fill_parent、wrap_content和match_parent的区别
查看>>
mysql笔记1------基础
查看>>
VC++ 中简单操作MP3音乐的方法,小结
查看>>
关于 BCSCTL1 = CALBC1_12MHZ;DCOCTL = CALDCO_12MHZ; 的疑问
查看>>
启动虚拟机时提示我已移动或我已复制选项的详解
查看>>
I.MX6 make menuconfig OTG to slave only mode
查看>>
代码抽象层次2
查看>>
Linux7 下重新安装YUM
查看>>
程序员修炼之道:从小工到专家
查看>>
WPF x名称空间
查看>>
【Quartz】【程序目录结构】/DetectNonWorkingDay/src/main/config/log4j.properties
查看>>
mac OS下显示.htaccess等隐藏文件
查看>>
北大校赛2011 Word Ladder(C题) 解题报告
查看>>
OsWorkFlow学习(FSM:有限状态机)
查看>>
根据二叉树的中序遍历和层次遍历还原二叉树
查看>>
paypal支付说明
查看>>
shell 命令 文件查看ls,复制cp,移动mv,查看文件内容cat more less,查看文件信息 file...
查看>>