我正在尝试将XML转换为列表
<School>
<Student>
<Id>2</Id>
<Name>dummy</Name>
<Section>12</Section>
</Student>
<Student>
<Id>3</Id>
<Name>dummy</Name>
<Section>11</Section>
</Student>
</School>我用LINQ尝试了几件事,但不太清楚该怎么做。
dox.Descendants("Student").Select(d=>d.Value).ToList();我得到count 2,但是值类似于2dummy12 3dummy11
是否可以将上述XML转换为具有Id、Name和Section Properties的类型为Student的泛型列表?
我能实现这一点的最佳方式是什么?
发布于 2013-04-30 18:26:49
您可以创建匿名类型
var studentLst=dox.Descendants("Student").Select(d=>
new{
id=d.Element("Id").Value,
Name=d.Element("Name").Value,
Section=d.Element("Section").Value
}).ToList();这将创建一个匿名类型的列表。
如果您想创建一个学生类型列表
class Student{public int id;public string name,string section}
List<Student> studentLst=dox.Descendants("Student").Select(d=>
new Student{
id=d.Element("Id").Value,
name=d.Element("Name").Value,
section=d.Element("Section").Value
}).ToList();https://stackoverflow.com/questions/16297583
复制相似问题