开发者

How to get parameter values from an XmlNode in C#

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="Mostl开发者_Go百科y Sunny"/>
</weather>

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


var node = xmldoc.SelectSingleNode("weather/weather-conditions");
var attr = node.Attributes["weather-summary"];


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜