Sitemap Creator V2 - C# Programming Exercise

In this exercise, you need to create a program that takes three parameters: the name of a text file containing the URLs, the modification date, and the change frequency. The program should generate a sitemap based on the provided data. The text file should contain a list of file names to be indexed, with each file name on a separate line. The program should read the text file, process the URLs, and generate a sitemap with the specified dates and frequencies in the proper format for search engine use. This exercise will help you work with text files, handle command line parameters, and generate content based on those parameters.

 Category

Additional Libraries

 Exercise

Sitemap Creator V2

 Objective

You need to create a program that takes the following parameters: the name of a text file containing the URLs, the modification date, and the change frequency.

Example: sitemapCreator urls.txt 2011-11-18 weekly

The text file should contain a list of file names to be indexed, with each file name on a separate line.

 Write Your C# Exercise

// Import necessary namespaces
using System;
using System.IO;
using System.Text;

class SitemapCreator
{
    // Main method where the program execution starts
    static void Main(string[] args)
    {
        // Check if the correct number of arguments is provided
        if (args.Length != 3)
        {
            Console.WriteLine("Usage: sitemapCreator   ");
            return;
        }

        // Get the parameters from the command line arguments
        string urlsFile = args[0];
        string modificationDate = args[1];
        string changeFrequency = args[2];

        // Check if the file exists
        if (!File.Exists(urlsFile))
        {
            Console.WriteLine("The specified file does not exist.");
            return;
        }

        // Read URLs from the file
        string[] urls = File.ReadAllLines(urlsFile);

        // Prepare the sitemap content
        StringBuilder sitemap = new StringBuilder();
        sitemap.AppendLine("");
        sitemap.AppendLine("");

        // Add each URL to the sitemap
        foreach (string url in urls)
        {
            sitemap.AppendLine("  ");
            sitemap.AppendLine($"    {url}");
            sitemap.AppendLine($"    {modificationDate}");
            sitemap.AppendLine($"    {changeFrequency}");
            sitemap.AppendLine("  ");
        }

        sitemap.AppendLine("");

        // Define the output file name
        string outputFile = "sitemap.xml";

        // Write the sitemap content to the file
        try
        {
            File.WriteAllText(outputFile, sitemap.ToString());
            Console.WriteLine($"Sitemap created successfully! The file is saved as {outputFile}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred while writing the sitemap: {ex.Message}");
        }
    }
}

 Share this C# exercise

 More C# Programming Exercises of Additional Libraries

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#.

  •  Surf directory

    In this exercise, you need to create a program that displays the files and folders in the current folder and allows the user to move up and down the list. If the user...

  •  Subdirectories

    In this exercise, you need to create a program that stores the files located in a specific folder and its subfolders. Then, the program should ask the user for a sear...

  •  Date and time

    In this exercise, you need to create a program that displays the current date and time in the following format:"Today is 6 of February of 2015. It´s 03:23:12".

  •  Display directory

    In this exercise, you need to create a program that shows all the files in the current folder. The program should list the files available in the directory where the program...

  •  Display executable files in directory

    In this exercise, you need to create a program that shows the names of all executable files (.com, .exe, .bat, .cmd) in the current folder, excluding the full path. The prog...

  •  Date and time continuous

    In this exercise, you need to create a program that displays the current time in the top-right corner of the screen with the format "HH:mm:ss". The program should pause for ...