Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using System.IO;
- using System.Security;
- /*Write a program that extracts from given HTML file its title (if available), and its body text without the HTML tags.
- * Example:
- * <html>
- * <head><title>News</title></head>
- * <body><p><a href="http://academy.telerik.com">
- * TelerikAcademy</a>aims to provide free real-world practical training for young people who want to turn into
- * skillful .NET software engineers.</p></body>
- * </html>
- */
- class ExctractsInfoFromHTML
- {
- static void Main()
- {
- string path = @"..\..\Files\html.html";
- try
- {
- string htmlFileContent = File.ReadAllText(path);
- string title = string.Empty;
- if (htmlFileContent.IndexOf("<title>") != -1)
- {
- int startTitle = htmlFileContent.IndexOf('>',htmlFileContent.IndexOf("<title>")) + 1;
- int endTitle = htmlFileContent.IndexOf("</title>");
- title = htmlFileContent.Substring(startTitle, endTitle - startTitle);
- }
- int endsOpenBodyTag = htmlFileContent.IndexOf(">",htmlFileContent.IndexOf("<body>")) + 1;
- StringBuilder bodyContent = new StringBuilder();
- for (int i = endsOpenBodyTag; i < htmlFileContent.Length; i++)
- {
- if (htmlFileContent[i] != '<')
- {
- bodyContent.Append(htmlFileContent[i]);
- }
- else
- {
- i = htmlFileContent.IndexOf('>', i);
- bodyContent.Append(" ");
- }
- }
- Console.WriteLine(title != string.Empty ? "Title: '{0}'" : "No title available.", title);
- Console.WriteLine("Body content: '{0}'", bodyContent.ToString());
- }
- catch (FileNotFoundException)
- {
- Console.WriteLine("The file specified in path({0}) was not found.", path);
- }
- catch (DirectoryNotFoundException)
- {
- Console.WriteLine("The specified path is invalid (for example, it is on an unmapped drive).");
- }
- catch (SecurityException)
- {
- Console.WriteLine("The caller does not have the required permission.");
- }
- catch (UnauthorizedAccessException)
- {
- Console.WriteLine("path specified a file that is read-only.");
- Console.WriteLine("Or This operation is not supported on the current platform.");
- Console.WriteLine("Or path specified a directory");
- Console.WriteLine("Or the caller does not have the required permission.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement