zoukankan      html  css  js  c++  java
  • C# 将对应的xml文档赋值给指定模型(对象)

      public static IList<T> XmlToEntityList<T>(string xml) where T : new()
            {
                XmlDocument doc = new XmlDocument();
                try
                {
                    doc.LoadXml(xml);
                }
                catch(Exception ex)
                {
                    ex.ToString().LogAllRecord();
                    return null;
                }

    return  EINModel<T>(doc);

    }

    private static IList<T> EINModel<T>(XmlDocument doc) where T : new()
            {
                XmlNode node = doc.ChildNodes[1];
                if (node == null) { return null; }
                XmlNode subNode = node.ChildNodes[1];
                IList<T> items = new List<T>();
                foreach (XmlNode a in subNode.ChildNodes)
                {
                    items.Add(XmlNodeToEntity<T>(a));
                }
                return items;
            }

            private static T XmlNodeToEntity<T>(XmlNode node) where T : new()
            {
                T item = new T();

                if (node.NodeType == XmlNodeType.Element)
                {
                    XmlElement element = (XmlElement)node;

                    System.Reflection.PropertyInfo[] propertyInfo =
                typeof(T).GetProperties(System.Reflection.BindingFlags.Public |
                System.Reflection.BindingFlags.Instance);

                    foreach (XmlNode attr in element.ChildNodes)
                    {
                        foreach (System.Reflection.PropertyInfo pinfo in propertyInfo)
                        {
                            if (pinfo != null)
                            {
                                if (pinfo.Name.ToLower() == attr.Name.ToLower())
                                {
                                    pinfo.SetValue(item, attr.InnerText, null);
                                    break;
                                }
                            }
                        }

                    }
                }
                return item;
            }

  • 相关阅读:
    Sencha的Eclipse插件提示和技巧
    《敏捷软件开发过程及最佳实践》培训总结
    《Sencha应用程序的UI测试 》一文的示例分析
    Ext JS 4.2 Beta版发布
    迅速解决resin或者tomcat启动闪一下就消失的问题
    import javax.servlet 出错
    有爱好者把我的CMS管理系统改成了JAVA版,有兴趣的可以看看
    一个@符号引发的血案:Access数据库无法更新
    Windows 7下如何安装和配置IIS 7和ASP
    .Net中Freetextbox_1.6.3的使用与ftb.imagegallery.aspx安全修正
  • 原文地址:https://www.cnblogs.com/LuoEast/p/10683020.html
Copyright © 2011-2022 走看看