简体   繁体   English

对于facebook喜欢的linq-xml查询

[英]linq-xml query for facebook likes

I'm trying to get the total count of facebook likes for links. 我试图得到Facebook喜欢链接的总数。 paste the link in your browser to see the xml response 将链接粘贴到浏览器中以查看xml响应

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. 它在linq查询中抛出一个对象引用查询。 How can i get the toal_count from the response? 如何从响应中获取toal_count?

Thanks 谢谢

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);
}

First of all total_Count has no descendants, so you can not query that if you want the value of that element. 首先,total_Count没有后代,因此如果您想要该元素的值,则无法查询。 Second, the resulting XML elements include a namespace. 其次,生成的XML元素包括命名空间。 You need to include these when searching the .value of that element. 在搜索该元素的.value时,您需要包含这些内容。 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM