zoukankan      html  css  js  c++  java
  • xml和对象 转换

    //测试数据
    static List<User> list = new List<User>()
            {
                new User(){id=1001	 ,name="语文",buyer="A"},
                new User(){id=1001	 ,name="语文",buyer="A"},
                new User(){id=1001	 ,name="数学",buyer="A"},
                new User(){id=1001	 ,name="数学",buyer="B"},
                new User(){id=1001	 ,name="数学",buyer="B"},
                new User(){id=1002	 ,name="英语",buyer="C"},
                new User(){id=1005	 ,name="英语",buyer="A"},
                new User(){id=1005	 ,name="英语",buyer="A"},
                new User(){id=1005	 ,name="英语",buyer="B"},
                new User(){id=1004	 ,name="计算机",buyer="A"},
                new User(){id=1007	 ,name="计算机",buyer="C"},
            };
    //实体模型
            public class User
            {
                public int id { get; set; }
                public virtual string name { get; set; }
                public virtual string buyer { get; set; }
                public virtual string idArr { get; set; }
                public virtual DateTime expiredTime { get; set; }
            }
    
            static void Main(string[] args)
            {
                //测试数据  xml
                string xml = XMLSerialize<List<User>>(list);
    
                //xml 转 对象
                List<User> ulist = XmlDeserialize<List<User>>(xml);
                //输出查看结果
                ulist.ForEach(x => Console.WriteLine(x.name));
    
                Console.ReadLine();
            }
    
            //序列化xml
            public static string XMLSerialize<T>(T entity)
            {
                StringBuilder buffer = new StringBuilder();
    
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                using (TextWriter writer = new StringWriter(buffer))
                {
                    serializer.Serialize(writer, entity);
                }
    
                return buffer.ToString();
    
            }
    
            //反序列化xml
            public static T XmlDeserialize<T>(string xmlString)
            {
                T myObject;
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                StringReader reader = new StringReader(xmlString);
                myObject = (T)serializer.Deserialize(reader);
                reader.Close();
                return myObject;
            }

  • 相关阅读:
    SpreadJS V13.0发布,聚焦表单设计与数据交互,让您的工作效率突飞猛进!
    springboot---redis缓存的使用
    springboot logback日志的使用
    Https与Http的区别以及Https的解说
    maven的使用解说
    Spring SpringMVC myBatis(简称SSM)理解
    抽象类与接口的区别
    Hadoop综合大作业
    hive基本操作与应用
    熟悉HBase基本操作
  • 原文地址:https://www.cnblogs.com/hanjun0612/p/9779755.html
Copyright © 2011-2022 走看看