M
mick
Been having a go at learning a bit about web services and WCF. Got that bit
working only
to get stuck at parsing the Response. I could simply parse the string myself but
I tried to
have a go at using LINQ to XML. Spent hours messing about with this last night
and still
couldn't work out how to reach particular elements and get their value. Still as
clueless
as when I started
So given that:
string res; //is the Response I got
var document = XElement.Parse(res)
or
var document = XDocument.Parse(res)
I'm not sure which one I'm supposed to use; any hints as to which and (in *very*
simple
terms), why?
Given the Response below, how would I get, for example, the value of the temp_C
Element?
If I wanted to get a collection of all of the Elements within the <data> Element
(that is, not
just its own child Element but every one between <data> and </data>), how would
I do that?
What are the major benefits of using Linq as opposed to just parsing the
response string
myself? Are their any other ways?
I use Linq to objects and XAML a lot but never tried to parse xml/xaml, and it
shows
<data>
<request>
<type>City</type>
<query>London, United Kingdom</query>
</request>
<current_condition>
<observation_time>11:11 AM</observation_time>
<temp_C>10</temp_C>
<temp_F>50</temp_F>
<weatherCode>113</weatherCode>
<weatherIconUrl><![CDATA[http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0001_sunny.png]]></weatherIconUrl>
<weatherDesc><![CDATA[Sunny]]></weatherDesc>
<windspeedMiles>14</windspeedMiles>
<windspeedKmph>22</windspeedKmph>
<winddirDegree>250</winddirDegree>
<winddir16Point>WSW</winddir16Point>
<precipMM>0.0</precipMM>
<humidity>76</humidity>
<visibility>10</visibility>
<pressure>1026</pressure>
<cloudcover>25</cloudcover>
</current_condition>
<weather>
<date>2013-11-29</date>
<tempMaxC>9</tempMaxC>
<tempMaxF>48</tempMaxF>
<tempMinC>3</tempMinC>
<tempMinF>37</tempMinF>
<windspeedMiles>16</windspeedMiles>
<windspeedKmph>25</windspeedKmph>
<winddirection>W</winddirection>
<winddir16Point>W</winddir16Point>
<winddirDegree>275</winddirDegree>
<weatherCode>119</weatherCode>
<weatherIconUrl><![CDATA[http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0003_white_cloud.png]]></weatherIconUrl>
<weatherDesc><![CDATA[Cloudy ]]></weatherDesc>
<precipMM>0.1</precipMM>
</weather>
</data>
working only
to get stuck at parsing the Response. I could simply parse the string myself but
I tried to
have a go at using LINQ to XML. Spent hours messing about with this last night
and still
couldn't work out how to reach particular elements and get their value. Still as
clueless
as when I started

So given that:
string res; //is the Response I got
var document = XElement.Parse(res)
or
var document = XDocument.Parse(res)
I'm not sure which one I'm supposed to use; any hints as to which and (in *very*
simple
terms), why?
Given the Response below, how would I get, for example, the value of the temp_C
Element?
If I wanted to get a collection of all of the Elements within the <data> Element
(that is, not
just its own child Element but every one between <data> and </data>), how would
I do that?
What are the major benefits of using Linq as opposed to just parsing the
response string
myself? Are their any other ways?
I use Linq to objects and XAML a lot but never tried to parse xml/xaml, and it
shows

<data>
<request>
<type>City</type>
<query>London, United Kingdom</query>
</request>
<current_condition>
<observation_time>11:11 AM</observation_time>
<temp_C>10</temp_C>
<temp_F>50</temp_F>
<weatherCode>113</weatherCode>
<weatherIconUrl><![CDATA[http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0001_sunny.png]]></weatherIconUrl>
<weatherDesc><![CDATA[Sunny]]></weatherDesc>
<windspeedMiles>14</windspeedMiles>
<windspeedKmph>22</windspeedKmph>
<winddirDegree>250</winddirDegree>
<winddir16Point>WSW</winddir16Point>
<precipMM>0.0</precipMM>
<humidity>76</humidity>
<visibility>10</visibility>
<pressure>1026</pressure>
<cloudcover>25</cloudcover>
</current_condition>
<weather>
<date>2013-11-29</date>
<tempMaxC>9</tempMaxC>
<tempMaxF>48</tempMaxF>
<tempMinC>3</tempMinC>
<tempMinF>37</tempMinF>
<windspeedMiles>16</windspeedMiles>
<windspeedKmph>25</windspeedKmph>
<winddirection>W</winddirection>
<winddir16Point>W</winddir16Point>
<winddirDegree>275</winddirDegree>
<weatherCode>119</weatherCode>
<weatherIconUrl><![CDATA[http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0003_white_cloud.png]]></weatherIconUrl>
<weatherDesc><![CDATA[Cloudy ]]></weatherDesc>
<precipMM>0.1</precipMM>
</weather>
</data>