• Model与XML互相转换


    #region Model与XML互相转换
            //// <summary>
            /// Model转化为XML的方法
            /// </summary>
            /// <param name="model">要转化的Model</param>
            /// <returns></returns>
            public static string ModelToXML(object model,string _method,string _type)
            {
                XmlDocument xmldoc = new XmlDocument();
                //加入XML的声明段落,<?xml version="1.0" encoding="gb2312"?>
                XmlDeclaration xmldecl;
                xmldecl = xmldoc.CreateXmlDeclaration("1.0", "gb2312", null);
                xmldoc.AppendChild(xmldecl);
                //加入一个根元素
                XmlElement ModelNode = xmldoc.CreateElement("", "message", "");
                //XmlElement ModelNode = xmldoc.CreateElement("message");
                ModelNode.SetAttribute("method", _method);//设置该message节点method属性
                ModelNode.SetAttribute("type", _type);//设置该message节点type属性
                xmldoc.AppendChild(ModelNode);

                if (model != null)
                {
                    //using System.Reflection;
                    foreach (PropertyInfo property in model.GetType().GetProperties())
                    {
                        XmlElement attribute = xmldoc.CreateElement(property.Name);
                        if (property.GetValue(model, null) != null)
                            attribute.InnerText = property.GetValue(model, null).ToString();
                        else
                            attribute.InnerText = "[Null]";
                        ModelNode.AppendChild(attribute);
                    }
                }
                return xmldoc.OuterXml;
            }

            //// <summary>
            /// XML转化为Model的方法
            /// </summary>
            /// <param name="xml">要转化的XML</param>
            /// <param name="SampleModel">Model的实体示例,New一个出来即可</param>
            /// <returns></returns>
            public static object XMLToModel(string xml, object SampleModel)
            {
                if (string.IsNullOrEmpty(xml))
                    return SampleModel;
                else
                {
                    XmlDocument xmldoc = new XmlDocument();
                    xmldoc.LoadXml(xml);

                    XmlNodeList attributes = xmldoc.SelectSingleNode("Model").ChildNodes;
                    foreach (XmlNode node in attributes)
                    {
                        foreach (PropertyInfo property in SampleModel.GetType().GetProperties())
                        {
                            if (node.Name == property.Name)
                            {
                                if (node.InnerText != "[Null]")
                                {
                                    if (property.PropertyType == typeof(System.Guid))
                                        property.SetValue(SampleModel, new Guid(node.InnerText), null);
                                    else
                                        property.SetValue(SampleModel, Convert.ChangeType(node.InnerText, property.PropertyType), null);
                                }
                                else
                                    property.SetValue(SampleModel, null, null);
                            }
                        }
                    }
                    return SampleModel;
                }
            }
            #endregion

  • 相关阅读:
    sky A800s手机恢复出厂设置操作
    SlimDx绘制点图元的问题
    自定义Token的CAS登录
    未来谁才是移动互联网的入口?
    [Oracle]Sqlplus连接成功,但pl/sql连接不成功,提示“ora-12145:无法解析指定的连接标识符”
    memcached分布式内存系统
    Android下结束进程的方法
    博客搬家啦~
    NOIp2013火柴排队
    NOIp2014 T2联合权值
  • 原文地址:https://www.cnblogs.com/sshh/p/1619567.html
Copyright © 2020-2023  润新知