4

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'.

2 Answers 2

7
var node = xmldoc.SelectSingleNode("weather/weather-conditions");
var attr = node.Attributes["weather-summary"];
Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.