TXT To HTML Translator - C# Programming Exercise

This C# exercise involves creating a "Text to HTML converter". The program should read a source text file and create an HTML file from its contents. The destination file should have the same name as the original file but with a ".html" extension instead of ".txt". Additionally, the title within the tag of the HTML file should be taken from the name of the source text file. This exercise is a great way to learn how to work with text and HTML files in C#, and it will help you understand how to generate HTML files dynamically from text files.

The program should read the content of the text file line by line and then generate an HTML file that contains each line of the text inside a

tag. The name of the text file should be used to create a title inside the tag. This exercise will teach you how to manipulate text strings and how to work with files in C#, as well as how to generate an HTML file with the proper structure.

At the end of this exercise, you will have a program capable of converting any text file into a well-structured HTML file, which can be useful in situations where you need to generate HTML content from plain text sources. This exercise is a practical way to practice file manipulation in C# and creating dynamic web content.

 Category

File Management

 Exercise

TXT To HTML Translator

 Objective

Create a "Text to HTML converter", which will read a source text file and create a HTML file from its contents. For example, if the file contains:

Hola
Soy yo
Ya he terminado

The name of the destination file must be the same as the source file, but with ".html" extension (which will replace the original ".txt" extension, if it exists). The "title" in the "head" must be taken from the file name.

 Write Your C# Exercise

// Import the System namespace for basic functionality
using System;  

// Import the IO namespace for file handling
using System.IO;  

// Create the class to handle text to HTML conversion
class TextToHtmlConverter
{
    // Main method where the program starts
    static void Main(string[] args)
    {
        // Ask the user for the source text file path
        Console.WriteLine("Enter the path of the text file:");  

        // Get the file path from the user
        string filePath = Console.ReadLine();  

        // Check if the file exists
        if (File.Exists(filePath))
        {
            // Get the name of the file without extension
            string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);

            // Create the destination HTML file path by replacing the .txt extension with .html
            string htmlFilePath = filePath.Replace(".txt", ".html");

            try
            {
                // Open the source text file and read all lines
                string[] lines = File.ReadAllLines(filePath);

                // Open the destination HTML file to write the content
                using (StreamWriter writer = new StreamWriter(htmlFilePath))
                {
                    // Write the HTML structure, starting with the  and  tags
                    writer.WriteLine("");
                    writer.WriteLine("");
                    writer.WriteLine($"{fileNameWithoutExtension}");  // Title taken from the file name
                    writer.WriteLine("");
                    writer.WriteLine("");

                    // Write each line from the text file as a paragraph in the HTML body
                    foreach (string line in lines)
                    {
                        writer.WriteLine($"

{line}

"); } // Close the body and html tags writer.WriteLine(""); writer.WriteLine(""); } // Inform the user that the conversion was successful Console.WriteLine("The text file has been successfully converted to HTML."); } catch (Exception ex) // Catch any errors that may occur { Console.WriteLine("An error occurred: " + ex.Message); } } else { // Inform the user if the file does not exist Console.WriteLine("The specified file does not exist."); } } }

 Share this C# exercise

 More C# Programming Exercises of File Management

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Invert binary file V2

    This C# exercise involves creating a program to "invert" a file using a FileStream. The program should create a file with the same name as the original file, b...

  •  BMP width & height, FileStream

    This C# exercise involves creating a program that reads a BMP file using a FileStream and displays its width and height. The BMP format has a spe...

  •  File copier

    This C# exercise involves creating a program that copies a source file to a destination file using a FileStream and a block size of 512 KB. The program should ...

  •  MP3 reader

    This C# exercise is about the ID3 specifications, which apply to any file or audiovisual container, but are primarily used with audio containers. There are three comp...

  •  C to C# converter

    This exercise consists of creating a program that converts simple C programs, such as the following one, into C#, ensuring that the resulting program compiles ...

  •  File splitter

    This exercise involves creating a program that splits a file (of any kind) into pieces of a certain size. The program should receive the file name and the desired size of th...