C# 将 XML 转换为通用列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16297583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:40:59  来源:igfitidea点击:

Converting a XML to Generic List

c#linqxml-parsing

提问by user2067567

I am trying to convert XML to List

我正在尝试将 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>

I tried few things using LINQ and am not so clear on proceeding.

我使用 LINQ 尝试了几件事,但对继续操作不太清楚。

dox.Descendants("Student").Select(d=>d.Value).ToList();

Am getting count 2 but values are like 2dummy12 3dummy11

正在计数 2 但值就像 2dummy12 3dummy11

Is it possible to convert the above XML to a generic List of type Student which has Id,Name and Section Properties ?

是否可以将上述 XML 转换为具有 Id、Name 和 Section Properties 的 Student 类型的通用列表?

What is the best way I can implement this ?

我可以实现的最佳方法是什么?

采纳答案by Anirudha

You can create an anonymous type

您可以创建一个匿名类型

var studentLst=dox.Descendants("Student").Select(d=>
new{
    id=d.Element("Id").Value,
    Name=d.Element("Name").Value,
    Section=d.Element("Section").Value
   }).ToList();

This creates a list of anonymous type..

这将创建一个匿名类型列表..



If you want to create a list of Student type

如果要创建 Student 类型的列表

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();

回答by Azhar Khorasany

var students = from student in dox.Descendants("Student")
           select new
            {
                id=d.Element("Id").Value,
                Name=d.Element("Name").Value,
                Section=d.Element("Section").Value
            }).ToList();

or you can create a class call Student with id, name and section as properties and do:

或者您可以创建一个类调用 Student,将 id、name 和 section 作为属性,然后执行以下操作:

var students = from student in dox.Descendants("Student")
           select new Student
            {
                id=d.Element("Id").Value,
                Name=d.Element("Name").Value,
                Section=d.Element("Section").Value
            }).ToList();

回答by Adil Mammadov

I see that you have accepted an answer. But I just want to show another way which I like. First you will need classes as below:

我看到你已经接受了一个答案。但我只想展示我喜欢的另一种方式。首先,您将需要以下课程:

public class Student
{
    [XmlElement("Id")]
    public int StudentID { get; set; }

    [XmlElement("Name")]
    public string StudentName { get; set; }

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

[XmlRoot("School")]
public class School
{
    [XmlElement("Student", typeof(Student))]
    public List<Student> StudentList { get; set; }
}

Then you can deserialize this xml:

然后你可以反序列化这个xml:

string path = //path to xml file

using (StreamReader reader = new StreamReader(path))
{
    XmlSerializer serializer = new XmlSerializer(typeof(School));
    School school = (School)serializer.Deserialize(reader);
}

Hope it will be helpful.

希望它会有所帮助。