Sitemap creator Learn programming Visual Basic (VB.net)

Lesson:

Additional Libraries


Exercise:

Sitemap creator


Objetive:

A "sitemap" is a file that webmasters can use to inform Google about the webpages that their website includes, with the aim of achieving better search engine rankings.

You must create a program that displays the contents of a preliminary "sitemap" on the screen. The sitemap should be generated from the list of ".html" files in the current folder, with a "weekly" frequency and the current date as the "last modification" date.


Code:

Imports System
Imports System.Collections.Generic
Imports System.IO
Class SitemapCreator
    Private Shared Sub Main()
        Dim ListHtml As List = GetHtml()
        CreateSiteMap(ListHtml, "weekly", DateTime.Now)
    End Sub

    Private Shared Sub CreateSiteMap(ByVal listHtml As List, ByVal frecuency As String, ByVal lastUpdated As DateTime)
        Try
            Dim writer As StreamWriter = New StreamWriter(File.Create("sitemap.xml"))
            writer.WriteLine("")
            writer.WriteLine("")

            For Each html As String In listHtml
                writer.WriteLine("")
                writer.WriteLine("" & html & "")
                writer.WriteLine("" & lastUpdated.ToShortDateString() & "")
                writer.WriteLine("" & frecuency & "")
                writer.WriteLine("")
            Next

            writer.WriteLine("")
            writer.Close()
        Catch
            Console.WriteLine("Error writing sitemap.")
        End Try
    End Sub

    Private Shared Function GetHtml() As List
        Dim ListHtml As List = New List()
        Dim files As String() = Directory.GetFiles(".")

        For Each file As String In files
            Dim extension As String = Path.GetExtension(file)

            Select Case extension
                Case ".html", ".htm"
                    ListHtml.Add(file.Substring(2))
            End Select
        Next

        Return ListHtml
    End Function
End Class

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.