0

我正在尝试获取链接的 Facebook 喜欢总数。将链接粘贴到浏览器中以查看 xml 响应

这是我正在做的事情:

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

它在 linq 查询中抛出一个对象引用查询。如何从响应中获取 toal_count?

谢谢

4

2 回答 2

1

首先,total_Count 没有后代,因此如果您想要该元素的值,则无法查询。其次,生成的 XML 元素包括名称空间。在搜索该元素的 .value 时,您需要包括这些。尝试这样的事情

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();
于 2012-09-12T15:54:05.527 回答
1

尝试,

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);
}
于 2012-09-12T15:31:30.470 回答