views:

163

answers:

3

Hi,

I need to read the woeid from the xml doc below. I need to read and store the data into a string variable so I can query the yahoo weather service.

XML returned by query:

<query yahoo:count="1"
       yahoo:created="2009-12-22T08:30:31Z"
       yahoo:lang="en-US"
       yahoo:updated="2009-12-22T08:30:31Z"
       yahoo:uri="http://query.yahooapis.com/v1/yql?q=select+woeid+from+geo.places+where+text%3D%22farnborough%2C+greater+london%2Cuk%22"&gt;
−
<diagnostics>
<publiclyCallable>true</publiclyCallable>
−
<url execution-time="32">
http://where.yahooapis.com/v1/places.q(farnborough%2C%20greater%20london%2Cuk);start=0;count=10
</url>
<user-time>33</user-time>
<service-time>32</service-time>
<build-version>4265</build-version>
</diagnostics>
−
<results>
−
<place>
<woeid>19941</woeid>
</place>
</results>
</query>

Can someone show me how to do this through linq?

----------EDIT ------------------------------------------------------------------------------------------

I've just realised i linq is not supported by .net 2.0...doh

So please could some suggest an alternative way using references available with .net 2.0? -maybe repost and tag? Many Thanks,

A: 

Here is a way to extract values from an xml file using Linq and xPath.

Hope this helps some.

Chris
+3  A: 

You can do it like this:

XDocument doc = XDocument.Parse(xml);
string s = doc.Descendants()
              .Where(element => element.Name == "woeid")
              .FirstOrDefault().Value;
Mark Byers
A question regrding this line:XDocument doc = XDocument.Parse(xml);i cannot pass it the xml document as it takes string. Can i pass it the xmldoc.ToString()?
nav
Maybe.... what is the type of your variable `xmldoc`?
Mark Byers
+1  A: 

You can use something similar to this Linq query to get the results back from the XML document

XDocument feeds = XDocument.Parse(xml);
var result = feeds.Descendants("diagnostics")
                    .Select(f => new 
                    {
                        UserTime = f.Element("uset-time").Value,
                        ServiceTime = f.Element("service-time").Value,
                        //... etc
                    }.First();
martani_net