LINQ to read XML

前端 未结 5 1950
逝去的感伤
逝去的感伤 2020-11-22 00:47

I got this XML file:


    
        
        
             


        
5条回答
  •  遥遥无期
    2020-11-22 01:42

    XDocument xdoc = XDocument.Load("data.xml");
    var lv1s = xdoc.Root.Descendants("level1"); 
    var lvs = lv1s.SelectMany(l=>
         new string[]{ l.Attribute("name").Value }
         .Union(
             l.Descendants("level2")
             .Select(l2=>"   " + l2.Attribute("name").Value)
          )
        );
    foreach (var lv in lvs)
    {
       result.AppendLine(lv);
    }
    

    Ps. You have to use .Root on any of these versions.

提交回复
热议问题