views:

24

answers:

2

I need to read the page keywords, is it possible to do with C# 3.5 ?

+1  A: 
string keywords = Page
    .Header
    .Controls
    .OfType<HtmlMeta>()
    .Where(meta => string.Equals(meta.Name, "keywords", StringComparison.OrdinalIgnoreCase))
    .Select(meta => meta.Content)
    .FirstOrDefault();
Darin Dimitrov
+1  A: 

Try this:

// Store the keywords here:
List<String> keywords = new List<String>();
foreach (Control c in this.Page.Header.Controls)
{
    HtmlMeta meta = c as HtmlMeta;
    if (meta != null &&
            String.Compare(meta.Name, "keywords", true, CultureInfo.InvariantCulture) == 0)
    {
        // When it is a Keywords meta tag, split the contents on each komma
        // and trim the spaces off.
        string[] kwds = (meta.Content.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries);
        foreach(string s in kwds)
            keywords.Add(s.Trim());
    }
}
return keywords;
Virtlink