I have the below code to get alexa page rank.
private int GetAlexaRank(string domain)
{
var alexaRank = 0;
try
{
var url = string.Format("http://data.alexa.com/data?cli=10&dat=snbamz&url={0}", domain);
var doc = XDocument.Load(url);
var rank = doc.Descendants("POPULARITY").Select(node => node.Attribute("TEXT").Value).FirstOrDefault();
if (!int.TryParse(rank, out alexaRank))
alexaRank = -1;
}
catch (Exception e)
{
return -1;
}
return alexaRank;
}
But I truly don't understand how does that code work??? Would you tell me exactly, what is the "POPULARITY"
and "TEXT"
? and where are they stored? I don't understand this syntax: doc.Descendants("POPULARITY").Select(node => node.Attribute("TEXT").Value).FirstOrDefault();
Please!!!!
I would recommend you navigating to the url
you have in your code and take a look at the XML
file structure. You should see tags with POPULARITY
and TEXT
, by the look of your code these are the nodes/attributes/values that you are selecting.
.Descendants
returns a collection, and since you provide POPULARITY
it will bring back the elements within the <POPULARITY>
tag in your XML file.
You are then looking at each node in the Collection
whose Descendants are Popularity
and selecting an item with attribute TEXT
. Your return the first value found, or null if the collection does not contain a node with attribute TEXT
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.