Sitemap creator v2 Learn programming Visual Basic (VB.net)

Lesson:

Additional Libraries


Exercise:

Sitemap creator v2 44


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.

You must create a program which will receive as parameters the name of a text file containing the URLs, the modification date and the frequency of changes:

sitemapCreator urls.txt 2011-11-18 weekly

The text file would contain the list of the names of the files to be indexed, each line in a different line.


Code:

Imports System
Imports System.IO
Imports System.Collections.Generic

Class SitemapCreator2
    Private Shared Sub Main(ByVal param As String())
        If param.Length <> 3 Then
            Console.WriteLine("Error number of params.")
            Return
        End If

        Dim file As String = param(0)
        Dim date As String = param(1)
        Dim frecuency As String = param(2)
        Dim ListUrls As List = GetUrls(file)
        CreateSiteMap(ListUrls, frecuency, date)
    End Sub

    Private Shared Sub CreateSiteMap(ByVal listHtml As List, ByVal frecuency As String, ByVal lastUpdated As String)
        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 & "")
                writer.WriteLine("" & frecuency & "")
                writer.WriteLine("")
            Next

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

    Private Shared Function GetUrls(ByVal nameFile As String) As List
        Try
            Dim reader As StreamReader = New StreamReader(File.OpenRead(nameFile))
            Dim line As String = ""
            Dim urls As List = New List()

            Do
                line = reader.ReadLine()

                If line IsNot Nothing Then
                    urls.Add(line)
                End If
            Loop While line IsNot Nothing

            reader.Close()
            Return urls
        Catch
            Console.WriteLine("Error reading file.")
            Return Nothing
        End Try
    End Function
End Class