zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    【Jmeter】 Report Dashboard 生成html图形测试报告
    【Python】更优的字符串格式化方式 -- "format"替代"%s"
    【Jmeter】压测mysql数据库中间件mycat
    UTF-8文件的Unicode签名BOM(Byte Order Mark)问题记录(EF BB BF)
    【Python】常用排序算法的python实现和性能分析
    【Python】模块学习之Timer定时任务,递归定时自调获取博客浏览量
    博客园文章添加阅读目录及CSS样式的方法总结
    【Python】解决Python脚本 在cmd命令行窗口运行时,中文乱码问题
    【Python】模块学习之locust性能测试
    【Python】模块学习之使用paramiko连接Linux,远程执行命令,上传下载、文件
  • 原文地址:https://www.cnblogs.com/sshh/p/1619567.html
Copyright © 2011-2022 走看看