views:

50

answers:

0

This is my class which doesn't seem to do anything. I got it from this website a few days back. My aim is to be able to call it and pass a number, which will then allow me to show only the number of words as specified in the call. e.g first 2000 words of a long string.

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HtmlAgilityPack;

public class GetSummary
{
    public GetSummary()
    {
    }

    public string MySummary(string html, int max)
    {
        string summaryHtml = string.Empty;

        // load our html document
        HtmlDocument htmlDoc = new HtmlDocument();
        htmlDoc.LoadHtml(html);

        int wordCount = 0;

        foreach (var element in htmlDoc.DocumentNode.ChildNodes)
        {
            // inner text will strip out all html, and give us plain text
            string elementText = element.InnerText;

            // we split by space to get all the words in this element
            string[] elementWords = elementText.Split(new char[] { ' ' });

            // and if we haven't used too many words ...

            if (wordCount <= max)
            {
                // add the *outer* HTML (which will have proper 
                // html formatting for this fragment) to the summary
                summaryHtml += element.OuterHtml;
                wordCount += elementWords.Count() + 1;
            }
            else
            {
                break;
            }
        }

        return summaryHtml;
    }
}