Practice Exercises C# Sharp - Learn to program with performing exercises C# Sharp

Practice Exercises C# Sharp

Learn to program with performing exercises C# Sharp


Sitemap creator - Practice Exercises C# Sharp


Lesson 12:

Additional libraries


Exercise 12.5:

Sitemap creator


Objetive:

A "sitemap" is a file that webmasters can use in order to tell Google the webpages that their site consists on, and get a better positioning in the search engine.

Its basic appearance is:

<?xml version="1.0" encoding="utf-8" ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc>http://miweb.com/</loc>
        <lastmod>2013-03-18</lastmod>
        <changefreq>daily</changefreq>
    </url>
    <url>
        <loc>http://miweb.com/apartado1</loc>
        <lastmod>2013-03-18</lastmod>
        <changefreq>daily</changefreq>
    </url>
</urlset>


Yo must create a program to show on screen the contents of a preliminary "sitemap", taken from the list of ".html" files in the current directory, with "weekly" frequence and the current date as "last modification" date.


Source Code:


using System;
using System.Collections.Generic;
using System.IO;
class SitemapCreator
{
    static void Main()
    {
        List ListHtml = GetHtml();

        CreateSiteMap(ListHtml, "weekly", DateTime.Now);
    }


    static void CreateSiteMap(List listHtml, string frecuency, DateTime lastUpdated)
    {
        try
        {
            StreamWriter writer = new StreamWriter(File.Create("sitemap.xml"));

            writer.WriteLine("");
            writer.WriteLine("");

            foreach (string html in listHtml)
            {
                writer.WriteLine("");
                writer.WriteLine("" + html + "");
                writer.WriteLine("" + lastUpdated.ToShortDateString() + "");
                writer.WriteLine("" + frecuency + "");
                writer.WriteLine("");
            }

            writer.WriteLine("");

            writer.Close();
        }
        catch
        {
            Console.WriteLine("Error writing sitemap.");
        }
    }

    static List GetHtml()
    {
        List ListHtml = new List();

        string[] files = Directory.GetFiles(".");

        foreach (string file in files)
        {
            string extension = Path.GetExtension(file);

            switch (extension)
            {
                case ".html":
                case ".htm":
                    ListHtml.Add(file.Substring(2));
                    break;
            }
        }

        return ListHtml;
    }
}
Exercisey 12.5




Privacy Policy:



Google uses associated advertising companies to serve ads when it visits our website. These companies may use the information they obtain from your visits to this and other websites (not including your name, address, email address, or phone number) to provide you with announcements about products and services that interest you. If you would like to learn more about this practice and know your options to prevent these companies from using this information. Click in... Privacy and Terms of Google.

Cookies

This site uses Google cookies to provide its services, to personalize advertisements and to analyze traffic. Google receives information about your use of this website. More information in... Privacy and Terms of Google.