Linq to XML Can't get Query Working

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Im trying to query the xml results passed back from the NOOA Weather webservice. For this example, Im trying to get the "hourly" weather value
Call:
<pre class="prettyprint string xml = localWeather.NDFDgen(lat, lng, NOAAWeather.productType.timeseries, DateTime.Now,
DateTime.Now, NOAAWeather.unitType.e, wtp);
XElement curXML = XElement.Parse(xml);[/code]
<br/>

Trimmed down version of whats returned:
<pre class="prettyprint dwml version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd
<head>
<product srsName="WGS 1984" concise-name="time-series" operational-mode="official
<title>NOAAs National Weather Service Real Time Mesoscale Analysis And Forecast Data</title>
<field>meteorological</field>
<category>analysis and forecast</category>
<creation-date refresh-frequency="PT1H 2012-06-27T15:47:23Z</creation-date>
</product>
<source>
<more-information>http://products.weather.gov/search.php</more-information>
<production-center>Meteorological Development Laboratory<sub-center>Product Generation Branch</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="43.63" longitude="-89.78" />
</location>
<moreWeatherInformation applicable-location="point1 http://forecast.weather.gov/MapClick.php?textField1=43.63&amp;textField2=-89.78</moreWeatherInformation>
<time-layout time-coordinate="local" summarization="none
<layout-key>k-p24h-n1-8</layout-key>
<start-valid-time>2012-06-27T07:00:00-05:00</start-valid-time>
<end-valid-time>2012-06-28T07:00:00-05: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>95</value>
</temperature>
<temperature type="hourly" units="Fahrenheit" time-layout="k-p3h-n1-3
<name>Temperature</name>
<value>90</value>
</temperature>
<cloud-amount type="total" units="percent" time-layout="k-p3h-n1-3
<name>Cloud Cover Amount</name>
<value>14</value>
</cloud-amount>
</parameters>
</data>
</dwml>[/code]
Ideally, I would like to write a query that gets the "name" and "value" child elements based on the parent elements "type" attribute. For example I want to return "Temperature" and "90" accordingly :
<temperature type="hourly" units="Fahrenheit" time-layout="k-p3h-n1-3 <br/>
<name>Temperature</name><br/>
<value>90</value><br/>
</temperature>
Ive tried getting at these values a couple of ways:
First attempt:
<pre class="prettyprint var temps =
from item in curXML.Descendants("temperature")
select item;

foreach (var temp in temps)
{
switch (temp.Attribute("type").Value)
{
case "hourly":
var curTemp = temp.Value - returns "temperature 90" which is wrong, just want the 90
var curTemp = temp.Element("Value") - doesnt work
break;
}
}[/code]
<br/>
Next, Ive been trying to query for the specific element instead of doing a foreach, but not sure what Im doing:
<pre class="prettyprint string t = "hourly";
var v = from weather in curXML.Descendants("temperature")
where t == weather.Element("temperature").Attribute("type").Value
select weather.Elements - not sure what I should be selecting[/code]
<br/>
Can someone give me some pointers? Thanks



<hr class="sig James

View the full article
 
Back
Top