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.yah开发者_开发百科ooapis.com/v1/yql?q=select+woeid+from+geo.places+where+text%3D%22farnborough%2C+greater+london%2Cuk%22">
−
<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,
You can do it like this:
XDocument doc = XDocument.Parse(xml);
string s = doc.Descendants()
.Where(element => element.Name == "woeid")
.FirstOrDefault().Value;
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();
Here is a way to extract values from an xml file using Linq and xPath.
Hope this helps some.
精彩评论