I Want to Parse XML with Linq.
这里是我的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; }
}
}
}
纽 约 请帮助研究如何将Adrress Into List in LINQ Query。 As You Can See There Are many Address Nodes Under The Person. 因此,我怎么能把所有地址都列入我的名单目标。