Reputation: 13
I am trying to get the values out of the Soap request I made. It comes in an XML format, for some reason I cant get just the values. How can I? How would I get just maximum and minimum temperature values?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WeatherData.Weather_Gov;
using System.Xml.Linq;
namespace WeatherData
{
class Program
{
static void Main(string[] args)
{
ndfdXMLPortTypeClient client = new ndfdXMLPortTypeClient();
weatherParametersType weatherParams = new weatherParametersType();
string weatherData;
decimal lat = (decimal)(31.5452);
decimal lon = (decimal)-109.468;
weatherData = client.NDFDgenByDay(lat, lon, DateTime.Now.ToUniversalTime().AddDays(-1), "2", "e", "12 hourly");
var myElement = XElement.Parse(weatherData);
Console.WriteLine(weatherData);
Console.ReadKey();
}
}
}
This is the output what I got in my console application window.
<?xml version="1.0"?>
<dwml version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/20
01/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.nws.noaa.gov/forecasts/xml/DWML
gen/schema/DWML.xsd">
<head>
<product srsName="WGS 1984" concise-name="dwmlByDay" operational-mode="official">
<title>NOAA's National Weather Service Forecast by 12 Hour Period</title>
<field>meteorological</field>
<category>forecast</category>
<creation-date refresh-frequency="PT1H">2015-06-01T15:16:52Z</creation-date>
</product>
<source>
<more-information>http://www.nws.noaa.gov/forecasts/xml/</more-information>
<production-center>Meteorological Development Laboratory<sub-center>Product Generation Bra
nch</sub-center></production-center>
<disclaimer>http://www.nws.noaa.gov/disclaimer.html</disclaimer>
<credit>http://www.weather.gov/</credit>
<credit-logo>http://www.weather.gov/images/xml_logo.gif</credit-logo>
<feedback>http://www.weather.gov/feedback.php</feedback>
</source>
</head>
<data>
<location>
<location-key>point1</location-key>
<point latitude="31.55" longitude="-109.47"/>
</location>
<moreWeatherInformation applicable-location="point1">http://forecast.weather.gov/MapClick.ph
p?textField1=31.55&textField2=-109.47</moreWeatherInformation>
<time-layout time-coordinate="local" summarization="12hourly">
<layout-key>k-p24h-n1-1</layout-key>
<start-valid-time period-name="Today">2015-06-01T06:00:00-07:00</start-valid-time>
<end-valid-time>2015-06-01T18:00:00-07:00</end-valid-time>
</time-layout>
<time-layout time-coordinate="local" summarization="12hourly">
<layout-key>k-p24h-n1-2</layout-key>
<start-valid-time period-name="Tonight">2015-06-01T18:00:00-07:00</start-valid-time>
<end-valid-time>2015-06-02T06:00:00-07:00</end-valid-time>
</time-layout>
<time-layout time-coordinate="local" summarization="12hourly">
<layout-key>k-p12h-n2-3</layout-key>
<start-valid-time period-name="Today">2015-06-01T06:00:00-07:00</start-valid-time>
<end-valid-time>2015-06-01T18:00:00-07:00</end-valid-time>
<start-valid-time period-name="Tonight">2015-06-01T18:00:00-07:00</start-valid-time>
<end-valid-time>2015-06-02T06:00:00-07:00</end-valid-time>
</time-layout>
<time-layout time-coordinate="local" summarization="12hourly">
<layout-key>k-p1d-n1-4</layout-key>
<start-valid-time>2015-06-01T06:00:00-07:00</start-valid-time>
<end-valid-time>2015-06-02T06:00:00-07:00</end-valid-time>
</time-layout>
<parameters applicable-location="point1">
<temperature type="maximum" units="Fahrenheit" time-layout="k-p24h-n1-1">
<name>Daily Maximum Temperature</name>
<value>94</value>
</temperature>
<temperature type="minimum" units="Fahrenheit" time-layout="k-p24h-n1-2">
<name>Daily Minimum Temperature</name>
<value>58</value>
</temperature>
<probability-of-precipitation type="12 hour" units="percent" time-layout="k-p12h-n2-3">
<name>12 Hourly Probability of Precipitation</name>
<value>4</value>
<value>2</value>
</probability-of-precipitation>
<weather time-layout="k-p12h-n2-3">
<name>Weather Type, Coverage, and Intensity</name>
<weather-conditions weather-summary="Mostly Sunny"/>
<weather-conditions weather-summary="Mostly Clear"/>
</weather>
<conditions-icon type="forecast-NWS" time-layout="k-p12h-n2-3">
<name>Conditions Icons</name>
<icon-link>http://www.nws.noaa.gov/weather/images/fcicons/few.jpg</icon-link>
<icon-link>http://www.nws.noaa.gov/weather/images/fcicons/nfew.jpg</icon-link>
</conditions-icon>
<hazards time-layout="k-p1d-n1-4">
<name>Watches, Warnings, and Advisories</name>
<hazard-conditions xsi:nil="true"/>
</hazards>
</parameters>
</data>
</dwml>
How do I pull out, for example, the Daily Max Temp?
<temperature type="maximum" units="Fahrenheit" time-layout="k-p24h-n1-1">
<name>Daily Maximum Temperature</name>
<value>94</value>
</temperature>
Upvotes: 0
Views: 595
Reputation: 26213
So once you've parsed your XML to an XElement
, you use the LINQ to XML API to query it:
var max = element.Descendants("temperature")
.Where(e => (string)e.Element("name") == "Daily Maximum Temperature")
.Select(e => (int)e.Element("value"))
.Single();
var min = element.Descendants("temperature")
.Where(e => (string)e.Element("name") == "Daily Minimum Temperature")
.Select(e => (int)e.Element("value"))
.Single();
If you had multiple values, then you could iterate through a sequence of integers:
var maxTemps = element.Descendants("temperature")
.Where(e => (string) e.Element("name") == "Daily Maximum Temperature")
.SelectMany(e => e.Elements("value"))
.Select(e => (int)e);
Upvotes: 2