将XML转换为通用列表进行中清除。dox.Descendants("Student").Select(d=>d.Value).ToList();我得到计数??2,但值类似于2dummy123dummy11是否可以将上述XML转换为具有Id、名称和部分属性类型为Student的通用列表?实现这一目标的最佳方式是什么?您可以创建一个匿名类型varstudentLst=dox.Descendants("Student").Select(d=>new{id=d.Element("Id").Value,Name=d.Element("Name").Value,Section=d.Element("Section").Value}).ToList();这将创建一个匿名类型列表。如果你想创建一个学生类型列表Student{id=d.Element("Id").Value,name=d.Element("Name").Value,section=d.Element("Section").Value}).ToList();我看到你已经接受了答案。但我只是想展示另一种我喜欢的方式。首先,您需要这样的类:publicclassStudent{[XmlElement("Id")]publicintStudentID{get;放;}[XmlElement("Name")]publicstringStudentName{get;放;}[XmlElement("Section")]publicintSection{get;放;}}[XmlRoot("School")]publicclassSchool{[XmlElement("Student",typeof(Student))]publicListStudentList{get;放;}}然后你可以反序列化这个xml:stringpath=//xml文件的路径学校school=(School)serializer.Deserialize(reader);}希望能帮助到你。varstudents=fromstudentindox.Descendants("Student")选择新的{id=d.Element("Id").Value,Name=d.Element("Name").Value,Section=d.Element("节").Value}).ToList();或者你可以创建一个classcallstudent,以classID,name,section为属性,执行:如果对你有用,需要了解更多C#学习教程,希望你多多关注—varstudents=fromdox.Descendants("Student")中的学生选择新学生{id=d.Element("Id")。Value,Name=d.Element("Name").Value,Section=d.Element("Section").Value}).ToList();本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
