Assume person is
public class Person
{
public string Name { get; set; }
public DateTime DateOfBirth { get; set; }
public string Department { get; set; }
}
Parsing:
var xdoc = XDocument.Load(path_to_xml);
var people = from p in xdoc.Root.Elements("Person")
select new Person {
Name = (string)p.Attribute("name"),
DateOfBirth = (DateTime)p.Element("DOB"),
Department = (string)p.Element("Department")
};
You can also use xml deserialization. Add serialization attributes:
public class Results
{
[XmlElement("Person")]
public List<Person> People { get; set; }
}
public class Person
{
[XmlAttribute("name")]
public string Name { get; set; }
[XmlElement("DOB")]
public DateTime DateOfBirth { get; set; }
public string Department { get; set; }
}
And deserialize results:
XmlSerializer serializer = new XmlSerializer(typeof(Results));
var results = (Results)serializer.Deserialize(File.OpenRead(path_to_xml));
var people = results.People;