开发者

how to display all tree child nodes using linq with xml data?

This is my xml file.I want to display all child nodes using linq.can any one give m开发者_StackOverflow社区e suggestion or answwer?


This will print out each block and all its fields:

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);
    }
}

like so:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜