0

我想用 Linq 解析 XML。

这是我的 XML 数据。

 <?xml version="1.0" encoding="utf-8" ?>
    <people>
      <person>
        <firstname>Kate</firstname>
        <lastname>Smith</lastname>
        <Address>Address</Address>
        <Address>Address2</Address>
        <Address>Address3</Address>
        <age>27</age>
      </person>
      <person>
        <firstname>Tom</firstname>
        <lastname>Brown</lastname>
        <Address>Address4</Address>
        <Address>Address5</Address>
        <Address>Address6</Address>
        <age>30</age>
      </person>
      <person>
        <firstname>Tim</firstname>
        <lastname>Stone</lastname>
        <Address>Address7</Address>
        <Address>Address8</Address>
        <Address>Address9</Address>
        <age>36</age>
      </person>
      <person>
        <firstname>Ann</firstname>
        <lastname>Peterson</lastname>
        <Address>Address10</Address>
        <Address>Address11</Address>
        <Address>Address12</Address>
        <age>27</age>
      </person>
    </people>

public class XmlParser
    {
        List<Person> List = new List<Person>();
        public XmlParser()
        {
            XDocument loadedData = XDocument.Load("Persons.xml");
            var data = (from query in loadedData.Descendants("person")
                select new Person
                {
                    FirstName = (string)query.Element("firstname"),
                    LastName = (string)query.Element("lastname"),
                    //Address = (List<string>)query.Element("Address"), // I Want To Load All Address Into My List
                    Age = (int)query.Element("age")
                }).ToList();
            List.AddRange(data);
        }

        public class Person
        {
            string firstname;
            string lastname;
            List<string> address;
            int age;

            public string FirstName
            {
                get { return firstname; }
                set { firstname = value; }
            }

            public string LastName
            {
                get { return lastname; }
                set { lastname = value; }
            }

            public List<string> Address
            {
                get { return address; }
                set { address = value; }
            }

            public int Age
            {
                get { return age; }
                set { age = value; }
            }

        }
    }

任何人都可以帮助我如何在 LINQ 查询中将地址添加到列表中。如您所见,person 下有许多地址节点。那么如何将所有地址添加到我的列表对象中。

4

1 回答 1

3

获取元素列表的一种方法是在原始 linq 查询中嵌套另一个 linq 查询。

var loadedData = XDocument.Load("Persons.xml");
var data = (from query in loadedData.Descendants("person")
            select new Person
            {
                FirstName = (string)query.Element("firstname"),
                LastName = (string)query.Element("lastname"),
                Address = (from address in query.Elements("Address") 
                           select address.Value).ToList(),
                Age = (int)query.Element("age")
            });

这种模式一直出现在 linq to xml 中,所以我会记住它。

于 2012-05-07T18:46:30.773 回答