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");

-

原文地址:https://www.cnblogs.com/runliuv/p/11926097.html