zoukankan      html  css  js  c++  java
  • C# xml 转 list 对象

    出处:https://blog.csdn.net/hanna_123/article/details/79229368

    <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")

  • 相关阅读:
    ubuntu后台运行命令
    jquery获取焦点位于的元素
    thymeleaf 处理模板为字符串
    以字符串形式获取excel单元格中的内容
    ajax 上传文件
    springboot logback
    javaService
    Assistant For Chess Cards
    E生活
    易兼职-找工作兼职平台
  • 原文地址:https://www.cnblogs.com/lq0418/p/13612570.html
Copyright © 2011-2022 走看看