tags:

views:

65

answers:

2

How do I get the values for parameters in a XmlNode tag. For example:

<weather time-layout="k-p24h-n7-1">
    <name>Weather Type, Coverage, and Intensity</name>
    <weather-conditions weather-summary="Mostly Sunny"/>
</weather>

I want to get the value for the parameter 'weather-summary' in the node 'weather-conditions'.

+5  A: 
var node = xmldoc.SelectSingleNode("weather/weather-conditions");
var attr = node.Attributes["weather-summary"];
Dean Harding
perfect. thanks!
Brian
+3  A: 

In the interest of completeness, the .Net 3.5 way should be given as well:

Assuming

XDocument doc = XDocument.Parse(@"<weather time-layout='k-p24h-n7-1'>
    <name>Weather Type, Coverage, and Intensity</name>
    <weather-conditions weather-summary='Mostly Sunny'/></weather>");

Then either

return doc.Element("weather").Element("weather-conditions").Attribute("weather-summary").Value;

Or

return doc.Descendants("weather-conditions").First().Attribute("weather-summary").Value;

Will give you the same answer.

Dathan