Sponsored Ad

Monday, April 5, 2010

How to Read an HTML file in C#

 

This utility help you to code the logic to read an HTML file. This code block usase the StreamReader to read the file and stores in Stringbuilder. One can use this to perform any manipulation operation on stringbuilder and write it back on HTML file.

This utility is also helpful to read and analyze the content of HTML file. In case you feel any problem, Please contact us or comment below.

 

        /// <summary>
        /// Purpose : To read a HTML file and return as stringbuilder object
        /// </summary>
        public static System.Text.StringBuilder ReadHtmlFile(string pHtmlFileNameWithFullPath)
        {
            System.Text.StringBuilder pSb = new System.Text.StringBuilder();

            try
            {
                // Create an instance of StreamReader to read from a file.
                // The using statement also closes the StreamReader.
                using (System.IO.StreamReader mSrHtml = new System.IO.StreamReader(pHtmlFileNameWithFullPath))
                {
                    string mLine;
                    // Read and display lines from the file until the end of
                    // the file is reached.
                    while ((mLine = mSrHtml.ReadLine()) != null)
                    {
                        pSb.Append(mLine);
                    }
                }
            }
            catch (Exception objError)
            {
                throw objError;
            }

            return pSb;
        }

0 comments:

Post a Comment

Sponsored Ad

Website Update

Followers