Reputation: 1
string url = "http://search.yahooapis.com/WebSearchService/V1/webSearch?appid=YahooDemo&query=fosil+fuel";
List<Resource> sites = new List<Resource>();
WebRequest request = WebRequest.Create(url);
using (WebResponse response = request.GetResponse())
{
// Get the response stream
StreamReader reader = new StreamReader(response.GetResponseStream());
XDocument xmlDoc = new XDocument();
xmlDoc = XDocument.Parse(reader.ReadToEnd());
var results = from q in xmlDoc.Descendants("Result")
select new
{
Url = q.Element("ClickUrl").Value,
Title = q.Element("Title").Value,
Date = q.Element("ModificationDate").Value,
};
foreach (var item in results)
{
sites.Add(new Resource(item.Title, item.Date, item.Url));
}
}
The yahoo xml looks like:
<ResultSet xsi:schemaLocation="urn:yahoo:srch http://api.search.yahoo.com/WebSearchService/V1/WebSearchResponse.xsd" type="web" totalResultsAvailable="134000" totalResultsReturned="10" firstResultPosition="1" moreSearch="/WebSearchService/V1/webSearch?query=fosil+fuel&appid=YahooDemo®ion=us">
<Result>
<Title>Fosil Fuel | Flickr - Photo Sharing!</Title>
<Summary>Fosil Fuel ... <a href="http://www.flickr.com/photos/thomashawk/3910488337/" title="Fosil Fuel by Thomas Hawk, on Flickr"><img src="http://farm3.static.flickr. ...</Summary>
<Url>http://www.flickr.com/photos/thomashawk/3910488337/</Url>
<ClickUrl>http://www.flickr.com/photos/thomashawk/3910488337/</ClickUrl>
<DisplayUrl>www.flickr.com/photos/thomashawk/3910488337/</DisplayUrl>
<ModificationDate>1298707200</ModificationDate>
<MimeType>text/html</MimeType>
<Cache>
<Url>http://uk.wrs.yahoo.com/_ylt=A0WTeekRk5dNbBEAx4zdmMwF;_ylu=X3oDMTBwZTdwbWtkBGNvbG8DZQRwb3MDMQRzZWMDc3IEdnRpZAM-/SIG=16k5dp83c/EXP=1301865617/**http%3A//66.218.69.11/search/cache%3Fei=UTF-8%26appid=YahooDemo%26query=fosil%2Bfuel%26u=www.flickr.com/photos/thomashawk/3910488337/%26w=fosil%2Bfuel%2Bfuels%26d=V6aQZ_bJWYzN%26icp=1%26.intl=us</Url>
<Size>119332</Size>
</Cache>
</Result>
</ResultSet>
What am I doing wrong? Everytime I try something new the debugger shos that my results are empty.
Upvotes: 0
Views: 1414
Reputation: 3769
Assuming you don't need to set up proxies or credentials for the WebRequest
you can simplify even further than Talljoe mentioned actually:
string url = "http://search.yahooapis.com/WebSearchService/V1/webSearch?appid=YahooDemo&query=fosil+fuel";
Func<string, XName> qualifiedName = name => XName.Get(name, "urn:yahoo:srch");
XDocument xmlDoc = XDocument.Load(url);
var results = from q in xmlDoc.Descendants(qualifiedName("Result"))
select new Resource(
q.Element(qualifiedName("ClickUrl")).Value,
q.Element(qualifiedName("Title")).Value,
q.Element(qualifiedName("ModificationDate")).Value);
return results.ToList();
EDIT - fixed bug in my original response (missing XName for accessing elements) and added a funky Func<>
to make reuse of namespace qualification. Works on my machine(TM).
Upvotes: 0
Reputation: 14827
You need to specify a namespace: xmlDoc.Descendants(XName.Get("Result", "urn:yahoo:srch"))
You can simplify your code a bit:
var url = "http://search.yahooapis.com/WebSearchService/V1/webSearch?appid=YahooDemo&query=fosil+fuel";
var request = WebRequest.Create(url);
using (var response = request.GetResponse())
{
// Get the response stream
var xmlDoc = XDocument.Load(response.GetResponseStream());
var results = from q in xmlDoc.Descendants(XName.Get("Result", "urn:yahoo:srch"))
select new Resource(
q.Element(XName.Get("Title", "urn:yahoo:srch").Value,
q.Element(XName.Get("ModificationDate", "urn:yahoo:srch").Value,
q.Element(XName.Get("ClickUrl", "urn:yahoo:srch").Value);
return results.ToList();
}
Upvotes: 1
Reputation: 39491
Problem is that "Result" has namespace "urn:yahoo:srch" that you are omitting. Try xmlDoc.Descendants(XName.Get("Result", "urn:yahoo:srch"))
and it will work
Upvotes: 0