如何获取XmlNode标记中的参数值。例如:
<weather time-layout="k-p24h-n7-1">
<name>Weather Type, Coverage, and Intensity</name>
<weather-conditions weather-summary="Mostly Sunny"/>
</weather>
我想在节点'weather-conditions'中获取参数'weather-summary'的值。
答案 0 :(得分:7)
var node = xmldoc.SelectSingleNode("weather/weather-conditions");
var attr = node.Attributes["weather-summary"];
答案 1 :(得分:3)
为了完整性,还应该给出.Net 3.5方式:
假设
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>");
然后
return doc.Element("weather").Element("weather-conditions").Attribute("weather-summary").Value;
或者
return doc.Descendants("weather-conditions").First().Attribute("weather-summary").Value;
会给你相同的答案。