Answers for "linq to xml"

C#
0

linq to xml

// Sample XML
<root>
    <level1 name="A">
        <level2 name="A1" />
        <level2 name="A2" />
    </level1>
    <level1 name="B">
        <level2 name="B1" />
        <level2 name="B2" />
    </level1>
    <level1 name="C" />
</root>

using System.Xml.Linq;

void Main()
{
    StringBuilder result = new StringBuilder();
    //Load xml
    XDocument xdoc = XDocument.Load("data.xml");
    //Run query
    var lv1s = from lv1 in xdoc.Descendants("level1")
               select new { 
                   Header = lv1.Attribute("name").Value,
                   Children = lv1.Descendants("level2")
               };
    //Loop through results
    foreach (var lv1 in lv1s) 
    {
       result.AppendLine(lv1.Header);
       foreach(var lv2 in lv1.Children)
       {
           result.AppendLine("     " + lv2.Attribute("name").Value);
       }
    }
    Console.WriteLine(result);
}
Posted by: Guest on July-11-2021

C# Answers by Framework

Browse Popular Code Answers by Language