Google SiteMap


S

shapper

Hello,

I am trying to convert an Asp.Net 2.0 XML sitemap file to a Google's
sitemap file.

I am posting the formats of both files.

1. How can I do the conversion?

2. And can I use an .ashx Asp.Net file that when the .ashx file is
called the Google XML file is generated?

Thank You,
Miguel

GOOGLE SiteMap

<?xml version="1.0" encoding="UTF-8"?>
< urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
< url>
< loc>http://www.mydomain.com/example.aspx</loc>
< lastmod>2005-01-01</lastmod>
< changefreq>daily</changefreq>
< priority>0.8</priority>
</url>
</urlset>

ASP.NET 2.0 Site Map

<?xml version="1.0" encoding="utf-8" ?>
<siteMap
xmlns = "http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode>
<siteMapNode
url = "~/example.aspx"
changefreq = "daily"
lastmod = "2005-01-01"
description = "My example page and its contents"
priority = "0.8"
title = "Example page" />
</siteMapNode>
</siteMap>

Note: description and title should be droped when converting from
Asp.Net to Google sitemap.
 
Ad

Advertisements

K

Ken Cox [Microsoft MVP]

Here's a handler that should get you going.

Ken
Microsoft MVP [ASP.NET]

<%@ WebHandler Language="VB" Class="googlesitemap" %>
' googlesitemap.ashx
' Adapted from a C# version in
' ASP.NET 2.0 Unleashed
' http://www.superexpert.com/Books/AspNet2Unleashed/Default.aspx
' by Ken Cox [MVP - ASP.NET]
Imports System.Web
Imports System.Xml
Imports System.Text
Imports System.IO

Public Class googlesitemap : Implements IHttpHandler
Private xmlwtr As XmlWriter
Public Sub ProcessRequest _
(ByVal context As HttpContext) _
Implements IHttpHandler.ProcessRequest
context.Response.ContentType = "text/xml"
Dim settings As XmlWriterSettings = _
New XmlWriterSettings()
settings.Encoding = Encoding.UTF8
settings.Indent = True
xmlwtr = XmlWriter.Create _
(context.Response.OutputStream, settings)
xmlwtr.WriteStartDocument()
xmlwtr.WriteStartElement _
("urlset", "http://www.google.com/schemas/sitemap/0.84")
' Add root node
AddUrl(SiteMap.RootNode)
' Add all other nodes
Dim nodes As SiteMapNodeCollection = _
SiteMap.RootNode.GetAllNodes()
For Each node As SiteMapNode In nodes
AddUrl(node)
Next node

xmlwtr.WriteEndElement()
xmlwtr.WriteEndDocument()
xmlwtr.Flush()
End Sub

Private Sub AddUrl(ByVal node As SiteMapNode)
' Skip empty Urls
If String.IsNullOrEmpty(node.Url) Then
Return
End If
' Skip remote nodes
If node.Url.StartsWith("http", True, Nothing) Then
Return
End If
' Open url tag
xmlwtr.WriteStartElement("url")
' Write location
xmlwtr.WriteStartElement("loc")
xmlwtr.WriteString(GetFullUrl(node.Url))
xmlwtr.WriteEndElement()
' Write last modified
xmlwtr.WriteStartElement("lastmod")
xmlwtr.WriteString(GetLastModified(node.Url))
xmlwtr.WriteEndElement()
' Write changefreq
xmlwtr.WriteStartElement("changefreq")
xmlwtr.WriteString("daily")
xmlwtr.WriteEndElement()
' Write priority
xmlwtr.WriteStartElement("priority")
xmlwtr.WriteString("0.8")
xmlwtr.WriteEndElement()
' Close url tag
xmlwtr.WriteEndElement()
End Sub

Private Function GetFullUrl _
(ByVal url As String) As String
Dim context As HttpContext = HttpContext.Current
Dim server As String = _
context.Request.Url.GetComponents _
(UriComponents.SchemeAndServer, UriFormat.UriEscaped)
Return Combine(server, url)
End Function

Private Function Combine _
(ByVal baseUrl As String, _
ByVal url As String) As String
baseUrl = baseUrl.TrimEnd(New Char() {"/"c})
url = url.TrimStart(New Char() {"/"c})
Return baseUrl & "/" & url
End Function

Private Function GetLastModified _
(ByVal url As String) As String
Dim context As HttpContext = HttpContext.Current
Dim physicalPath As String = _
context.Server.MapPath(url)
Return File.GetLastWriteTimeUtc _
(physicalPath).ToString("s")
End Function

Public ReadOnly Property IsReusable() _
As Boolean Implements IHttpHandler.IsReusable
Get
Return True
End Get
End Property

End Class
 
Ad

Advertisements

S

shapper

Hi,

I saw the links. However, I am trying to do the following:
Each web project will have only one web site map file for each culture.
Inside each node of the web site map I add a attribute such as:

Google=True
or
Footer=True

This way I will be able to use those values to generate Google XML web
sites maps on the fly with an ashx file or even get nodes from the web
site map in my runtime code to fill the footer.

This way I don't have to mantain many web sitemaps.

This is way I am trying to figure out how to make the XML conversion
with XLST and using conditions to.

Thanks,
Miguel
 

Top