how to get all nodes and its attributes in XML with LINQ

I meam for example I can get

var collection = from c in el.Descendants("current_conditions")
                 select c;

only node - current_conditions with its attributes.

But how to get other nodes (forecast_conditions) without new query again(Is it possible to do it in one query?)? XML doc looks like this

<current_conditions>
 <condition data="Clear"/>
 <temp_f data="36"/>
 <temp_c data="2"/>
 <humidity data="Humidity: 70%"/>
 <icon data="/ig/images/weather/sunny.gif"/>
 <wind_condition data="Wind: N at 9 mph"/>
</current_conditions>
<forecast_conditions>
  <day_of_week data="Fri"/>
  <low data="28"/>
  <high data="54"/>
  <icon data="/ig/images/weather/mostly_sunny.gif"/>
  <condition data="Mostly Sunny"/> 
</forecast_conditions>

Upvotes: 0

Views: 295

Answers (1)

DotNetUser
DotNetUser

Reputation: 6612

If you do doc.Element for root element you get all the element nodes under the root.

    var xElement = doc.Element(rootElementName);

To get all the element nodes under any element, you can use Elements()

    XElement elem = xElement.Element(anyElementName);
    IEnumerable<XElement> nodeCollection = from allNodes in elem.Elements()
                                                            select allNodes;

Upvotes: 1

Related Questions