出处: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")