CSharp · Console
using System;
using System.Xml;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;
public class Program
{
public class People
{
public string Name { get; set; }
public string Age { get; set; }
public IList<Hobbies> Hobby { get; set; }
}
public class Hobbies
{
public string Title { get; set; }
public string Description { get; set; }
}
public static void Main()
{
string xml = @" <people>
<person>
<name>Mr Brown</name>
<age>40</age>
<hobby>
<title>Eating</title>
<description>eats a lot</description>
</hobby>
<hobby>
<title>Sleeping</title>
<description>sleeps a lot</description>
</hobby>
</person>
<person>
<name>Mr White</name>
<age>40</age>
<hobby>
<title>Skiing</title>
<description>more details on this</description>
</hobby>
<hobby>
<title>Football</title>
<description>watches football</description>
</hobby>
</person>
</people>";
XElement element=XElement.Parse(xml);
var person = from a in element.Descendants("person")
select new People
{
Name = a.Element("name").Value,
Age = a.Element("age").Value,
Hobby = a.Descendants("hobby")
.Select(x => new Hobbies
{
Title = x.Element("title").Value,
Description = x.Element("description").Value
}).ToList()
};
foreach(var d in person)
{
Console.WriteLine(String.Format("Name = {0}:Age={1}:Hobbies:{2}",d.Name,d.Age,d.Hobby.Count()));
}
}
}