• C# xml 转 list 对象


    <classes>
    <class>
    <classname>高三一班</classname>
    <id>1</id>
    <remark>学霸</remark>
    <students>
    <student>
    <name>爱因斯坦</name>
    <sex>男</sex>
    <age>18</age >
    </student>
    <student>
    <name>牛顿</name>
    <sex>男</sex>
    <age>18</age >
    </student>
    </students>
    </class>
    <class>
    <classname>高三Ⅹ班</classname>
    <id>1</id>
    <remark>渣渣</remark>
    <students>
    <student>
    <name>特朗普</name>
    <sex>男</sex>
    <age>18</age >
    </student>
    <student>
    <name>本</name>
    <sex>男</sex>
    <age>18</age >
    </student>
    </students>
    </class>
    </classes>
    C# 类
    [Serializable, XmlType("class")]
    public class ClassSet
    {
    [XmlElement("classname")]
    public string Name { get; set; }

    [XmlElement("id")]
    public int Id { get; set; }

    [XmlElement("remark")]
    public string Remark { get; set; }

    [XmlArray("students")]
    public List<Student> Students { get; set; }

    [Serializable, XmlType("student")]
    public class Student
    {
    [XmlElement("name")]
    public string Name { get; set; }

    [XmlElement("sex")]
    public string Sex { get; set; }

    [XmlElement("age")]
    public int Age { get; set; }

    }
    }
    C# XmlToList
    public static List<T> XmlToList<T>(string xml,string rootName) where T : class
    {
    XmlSerializer serializer = new XmlSerializer(typeof(List<T>), new XmlRootAttribute(rootName));
    using (StringReader sr = new StringReader(xml))
    {
    List<T> list = serializer.Deserialize(sr) as List<T>;
    return list;
    }
    }
    调用
    XmlToList<ClassSet>(xml, "classes")

  • 相关阅读:
    ajax实现无刷新上传附件并且显示进度条的实例
    thinkphp ajax 无刷新分页效果的实现
    微信错误码详述
    eclispse修改项目项目编码
    构建高性能web之路------mysql读写分离实战
    Hibernate 的Ehache学习
    sessionStorage和localStorage
    sql中的group by 和 having 用法解析
    Hibernate DetachedCriteria实现
    JavaScript eval_r() 函数
  • 原文地址:https://www.cnblogs.com/zhyp/p/15715776.html
Copyright © 2020-2023  润新知