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

Lesson:

Additional Libraries


Exercise:

Sitemap creator v2


Objetive:

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.


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

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