LINQ query to parse content from XML to class

前端 未结 3 1183
广开言路
广开言路 2021-01-28 13:35

I have an xml from which I am trying to extract some information through LINQ query.
The format of the xml file looks like the following:


           


        
3条回答
  •  没有蜡笔的小新
    2021-01-28 14:11

    var xDoc = XDocument.Load(....);
    var result = xDoc.Descendants("NextItem")
      .Union(xDoc.Descendants("PreviousItem"))
      .Select(n => new {ID = n.Attribute("id").Value, Name =n.Name, Value =n.Value });
    

    --EDIT--

    var result = XDocument.Load(....)
                .Descendants("Sequences")
                .Select(n=> n.Descendants("NextItem")
                     .Union(n.Descendants("PreviousItem"))
                     .Select(n2 => new { ID = n2.Attribute("id").Value, Name = n2.Name, Value = n2.Value })
                );
    

提交回复
热议问题