raklos
raklos

Reputation: 28545

linq-xml query for facebook likes

I'm trying to get the total count of facebook likes for links. paste the link in your browser to see the xml response

Heres's what I'm doing:

var link = "https://api.facebook.com/method/fql.query?query=select%20%20like_count,%20total_count,%20share_count,%20click_count%20from%20link_stat%20where%20url=%22http://micgadget.com/29723/the-undercover-report-on-how-the-new-iphone-5-is-made-inside-foxconn-factory/%22";
//var xml = MakeRequest(link);
XDocument xdoc = XDocument.Load(link);

var likes = (from e in xdoc.Descendants("total_count")
             select e).SingleOrDefault().Value;
return int.Parse(likes);

It's throwing an object reference query at the linq query. How can i get the toal_count from the response?

Thanks

Upvotes: 0

Views: 158

Answers (2)

Jignesh Thakker
Jignesh Thakker

Reputation: 3698

Try,

var link = "https://api.facebook.com/method/fql.query?query=select%20%20like_count,%20total_count,%20share_count,%20click_count%20from%20link_stat%20where%20url=%22http://micgadget.com/29723/the-undercover-report-on-how-the-new-iphone-5-is-made-inside-foxconn-factory/%22";
//var xml = MakeRequest(link);
XDocument xdoc = XDocument.Load(link);

var query =  xdoc.Descendants().SingleOrDefault(o => o.Name.LocalName == "total_count");

if (!(query == null))
{
    int count = int.Parse(query.Value);
}

Upvotes: 1

tazyDevel
tazyDevel

Reputation: 536

First of all total_Count has no descendants, so you can not query that if you want the value of that element. Second, the resulting XML elements include a namespace. You need to include these when searching the .value of that element. Try something like this

var link = "https://api.facebook.com/method/fql.query?query=select%20%20like_count,%20total_count,%20share_count,%20click_count%20from%20link_stat%20where%20url=%22http://micgadget.com/29723/the-undercover-report-on-how-the-new-iphone-5-is-made-inside-foxconn-factory/%22";
XDocument xdoc = XDocument.Load(link);
XNamespace ns = xdoc.Root.Attribute("xmlns").Value;

var likes = (from e in xdoc.Descendants() where e.Name == ns + "total_count"
             select e.Value).SingleOrDefault();

Upvotes: 1

Related Questions