How to get parameter values โ€‹โ€‹from XmlNode in C #

How to get values โ€‹โ€‹for parameters in an XmlNode tag. For instance:

<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 'weather-summary' parameter in node conditions.

+4
source share
2 answers
 var node = xmldoc.SelectSingleNode("weather/weather-conditions"); var attr = node.Attributes["weather-summary"]; 
+7
source

In the interest of completeness, the .Net 3.5 path should also be specified:

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; 

You will get the same answer.

+3
source

Source: https://habr.com/ru/post/1305061/


All Articles