Click here to Skip to main content
Click here to Skip to main content

Making Google XML SiteMaps From Files

, 10 Jan 2006
Rate this:
Please Sign up or sign in to vote.
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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Hamed J.I
Web Developer
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
I began programming with C/C++ when i was 15. Then try to learn VC++ but at the middle of my reading .NET came.
 
I began to read C# and VB.NET and also began designing basic websites by FrontPage and developed some websites for our school and some other companis.
 
Later learn Microcontroller and design some digital circuits with PIC microcontrollers for a industrial controller company.
 
As I learned SQL and ASP.NET developed some website such as news portals that are active now.
 
Now i'm a software student and teach programming in computer institues. And have my own job by getting projects from companies.

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 11 Jan 2006
Article Copyright 2006 by Hamed J.I
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid