zoukankan      html  css  js  c++  java
  • C#.NET XML 与 实体 MODEL 互转,非序列化

    只能处理简单结构XML 和 实体。

      

      

    using System.Text;
    using System.Xml;
    
    namespace A.Util
    {
        public static class MyXmlUtil
        {
            public static string ModelToXml<T>(T a ,string xmlRootName)
            {
                StringBuilder scXml = new StringBuilder();
                scXml.AppendFormat("<{0}>", xmlRootName);
    
    
                #region 主体
                System.Reflection.PropertyInfo[] cfgItemProperties = a.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
    
                foreach (System.Reflection.PropertyInfo item in cfgItemProperties)
                {
                    string name = item.Name;
                    object value = item.GetValue(a, null);
                    //string 或 值属性,且value 不为 null
                    if ((item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String")) && value != null && !string.IsNullOrEmpty(value.ToString()))
                    {
                        scXml.AppendFormat("<{0}>{1}</{0}>", name, value.ToString());
                    }
                }
    
                #endregion
    
                scXml.AppendFormat("</{0}>", xmlRootName);
    
                string xml = scXml.ToString();
    
                return xml;
            }
    
            public static void XmlToModel<T>(T a,string xmlContent, string xmlRootName)
            {
                xmlContent = xmlContent.Trim();
    
                //去除XML HEADER,否则LoadXml 时出错
                if (xmlContent.Contains("<?"))
                {
                    int bb = xmlContent.IndexOf('>');
                    xmlContent = xmlContent.Substring(bb + 1);
                }
                
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.XmlResolver = null;//2018-12-3
                xmlDoc.LoadXml(xmlContent);
                XmlNode root = xmlDoc.SelectSingleNode(xmlRootName);
                XmlNodeList xnl = root.ChildNodes;
    
                System.Reflection.PropertyInfo[] cfgItemProperties = a.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
    
                foreach (XmlNode xnf in xnl)
                {
                    //xnf.Name, xnf.InnerText
                    if (string.IsNullOrEmpty(xnf.InnerText))
                    {
                        continue;
                    }
    
                    #region 主体
                    
    
                    foreach (System.Reflection.PropertyInfo item in cfgItemProperties)
                    {
                        string name = item.Name;
                        //string 或 值属性 
                        if ( item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String") )
                        {
                            if (item.Name == xnf.Name    )
                            {
                                item.SetValue(a, xnf.InnerText, null);
                            }
                        }
                    }
    
                    #endregion
                }
                 
            }
        }
    }

    --

    使用

    VersChkHttpRsp vchrsp = new VersChkHttpRsp();
    MyXmlUtil.XmlToModel<VersChkHttpRsp>(vchrsp, rspXml, "xml");

    -

  • 相关阅读:
    微信公众平台消息接口开发之校验签名与消息响应合并
    微信公众平台开发之在网页上添加分享到朋友圈,关注微信号等按钮
    微信公众平台自定义菜单PHP开发
    所有边权均不相同的无向图最小生成树是唯一的证明
    无向带权图的最小生成树算法——Prim及Kruskal算法思路
    排序二叉树,平衡二叉树和红黑树的概念以及相关的操作讲解
    B树、B-树、B+树、B*树
    森林、树与二叉树相互转换
    普通树转换成二叉树
    哈夫曼树
  • 原文地址:https://www.cnblogs.com/runliuv/p/11926097.html
Copyright © 2011-2022 走看看