65.9K
CodeProject is changing. Read more.
Home

Making Google XML SiteMaps From Files

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.86/5 (3 votes)

Jan 11, 2006

1 min read

viewsIcon

24184

downloadIcon

179

An application to make Google XML sitemaps from file structures.

Introduction

This application can make XML files for the Google sitemap. You need to only get the path of your web site and the URL of the site, and then save the XML file where you want.

How it Works

At the Path tab, you give the address of the main directory of your web and the URL of your website.

At the Extension tab, you must configure what kind of files your project will contain. The application has to know the type of extensions that must be listed in the XML file.

Then in the File tab, after clicking "Read from directory", the program will start to search the directory for file extensions you configured in the Extension tab. The program will ignore the directories that starting with '_' in their names, because they are private directories and the browser can't show them. The application gets the file path and the last write time of the file, and leave other settings unchecked.

Because some pages can have query information, you must check the "This page has query" option.

In the Query tab, you must configure the queries for the pages, and then at the Save tab, save the XML file.

How the Code Works

We have a class "siteMap" that contains the information for the pages. This class can make the part of the XML file that is related to this file; this means all the files with the GetXML method returns a part of the XML file. All of this will be merged together to make the main XML file. This is the GetXML method:

Public ReadOnly Property GetXML() As String
    Get
        Dim Str As String
        Str = "<url>" & vbCrLf
        Str &= "<loc>" & URL & Loc & "</loc>"
        Loc = EscapeCode(Loc)
        If CheckLastMod = True Then
            Dim ModStr As String = LastMod.Date.ToShortDateString
            ModStr = ModStr.Replace("/", "-")
            Str &= vbCrLf & "<lastmod>" & ModStr
            If CheckLMTime = True Then
                Str &= "T" & GetTime() & "+00:00"
            End If
            Str &= "</lastmod>"
        End If
        If CheckChangeFreq = True Then
            Str &= vbCrLf
            Str &= "<changefreq>" & ChangeFreq.ToString() & "</changefreq>"
        End If
        If CheckPriority = True Then
            Str &= vbCrLf
            Str &= "<priority>" & Priority & "</priority>"
        End If
        Str &= vbCrLf & "</url>"
        Return Str
    End Get
End Property

The AddRootFiles function adds the files to the list box:

Private Sub AddRootFiles(ByVal Path As String)
    'Main Directory of files
    Dim Dir As DirectoryInfo
    Try
        Dir = New DirectoryInfo(Path)
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Local directory error", 
                MessageBoxButtons.OK, MessageBoxIcon.Error)
        Exit Sub
    End Try

    If Dir.Exists = False Then
        MessageBox.Show("The local directory path" & _ 
                        " not exist. please choose" & _ 
                        " address for local directory" & _ 
                        " on Path tab.", _
                        "local directory not exist", _
                        MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        Exit Sub
    End If

    Dim File As FileInfo
    Dim Ext As String
    If txtLocalPath.Text.Chars(txtLocalPath.Text.Length_
                                       - 1) <> "\" Then
        txtLocalPath.Text &= "\"
    End If
    For Each File In Dir.GetFiles
        For Each Ext In lsbExtension.Items
            If ("." & Ext).ToLower = _
                      File.Extension.ToLower Then
                If lsbFiles.Items.IndexOf("/" & _
                            File.Name) = -1 Then
                    Dim str As String = _
                       File.FullName.Replace(txtLocalPath.Text, "/")
                    Dim NewMap As New SiteMap
                    NewMap.CheckChangeFreq = False
                    NewMap.CheckLastMod = True
                    NewMap.CheckLMTime = True
                    NewMap.CheckPriority = False
                    NewMap.CanShowWithoutQ = True
                    NewMap.LastMod = File.LastWriteTime()

                    NewMap.Loc = str.Replace("\", "/")
                    lsbFiles.Items.Add(NewMap)
                    Addresses.Add(NewMap)
                End If
            End If
        Next
    Next
    Dim SDir As DirectoryInfo
    For Each SDir In Dir.GetDirectories
        If Not SDir.Name.StartsWith("_") Then
            AddRootFiles(SDir.FullName)
        End If
    Next

    UpdateView()
End Sub