Будет распечатан каждый блок и все его поля:
var tree = XElement.Parse(text);
foreach(var farm in tree.Descendants("farm"))
{
Console.WriteLine(farm.Attribute("Name").Value);
// print the blocks and their fields
foreach(var block in farm.Descendants("block"))
{
Console.WriteLine("\t{0}", block.Attribute("Name").Value);
foreach(var field in block.Descendants("field"))
Console.WriteLine("\t\t{0}", field.Attribute("Name").Value);
}
// print out the remaining fields that don't belong to a block
foreach(var field in farm.Descendants("field"))
{
Console.WriteLine("\t{0}", field.Attribute("Name").Value);
}
}
примерно так:
Farm A
Field 1
Farm B
Block North
Field 11
Field 12
Block South
Field 25
Field 26
Field 27
Field 28
Field 11
Field 12
Field 25
Field 26
Field 27
Field 28