Creazione di una Sitemap XML da SQL Server in ASP NET
Di
Ho voluto creare una mappa del sito per il mio sito web e ho deciso che XML era il miglior formato per i motori di ricerca da quello che ho potuto vedere su Wikipedia . Insieme a alcuni altri articoli che ho trovato sul web, mi metto in su con il codice qui sotto. Dovrebbe essere integrato senza problemi in un'applicazione con inclusione in un file robots.txt.
XML
<url>
<loc>https://www.claytabase.co.uk/en/About</loc>
<lastmod>2013-12-13</lastmod>
<changefreq>monthly</changefreq>
<priority>1</priority>
</url>
<url>
<loc>https://www.claytabase.co.uk/en/Articles</loc>
<lastmod>2013-12-09</lastmod>
<changefreq>monthly</changefreq>
<priority>1</priority>
</url>
</urlset>
Di
VB
Imports System.Data.SqlClient
Imports System.Xml
Imports System.Data
Imports System.Data.Sql
Partial Class SiteMap
Inherits System.Web.UI.Page
Dim con As New SqlConnection("{YourSQLConnection}")
Private SubLoadFeed() Handles Me.Load
Dim d As DateTime = DateTime.Now
Response.Clear()
Response.ContentType = "text/xml"
Dim xtwFeed As XmlTextWriter = NewXmlTextWriter(Response.OutputStream, Encoding.UTF8)
xtwFeed.WriartDocument()
xtwFeed.WriartElement("urlset")
xtwFeed.WriteAttriburing("xmlns","https://www.sitemaps.org/schemas/sitemap/0.9")
xtwFeed.WriartElement("url")
xtwFeed.WriteElementString("loc","https://www.domain.com/Home/")
xtwFeed.WriteElementString("lastmod",String.Concat(d.ToString("yyyy-MM-dd")))
xtwFeed.WriteElementString("changefreq","monthly") 'always, hourly, daily, weekly, monthly, yearly, never
xtwFeed.WriteElementString("priority","1")
xtwFeed.WriteEndElement()
xtwFeed.WriartElement("url")
xtwFeed.WriteElementString("loc","https://www.domain.com/About/")
xtwFeed.WriteElementString("lastmod",String.Concat(d.ToString("yyyy-MM-dd")))
xtwFeed.WriteElementString("changefreq","monthly")
xtwFeed.WriteElementString("priority","1")
xtwFeed.WriteEndElement()
xtwFeed.WriartElement("url")
xtwFeed.WriteElementString("loc","https://www.domain.com/Blog/")
xtwFeed.WriteElementString("lastmod",String.Concat(d.ToString("yyyy-MM-dd")))
xtwFeed.WriteElementString("changefreq","monthly")
xtwFeed.WriteElementString("priority","1")
xtwFeed.WriteEndElement()
xtwFeed.WriartElement("url")
xtwFeed.WriteElementString("loc","https://www.domain.com/WebDesign/")
xtwFeed.WriteElementString("lastmod",String.Concat(d.ToString("yyyy-MM-dd")))
xtwFeed.WriteElementString("changefreq","monthly")
xtwFeed.WriteElementString("priority", "1")
xtwFeed.WriteEndElement()
Dim com As New SqlCommand("SELECT{YourLoc},{YourDateMod},{YourChangeFreq},{YourPriority} FROM{YourDatabase}", con)
con.Open()
Dim dr = com.ExecuteReader
While dr.Read
xtwFeed.WriartElement("url")
xtwFeed.WriteElementString("loc","https://www.domain.com/Articles/afdasf/"+ dr.Item(0).ToString) 'OR full URL from yourdatabase!
xtwFeed.WriteElementString("lastmod",String.Concat(dr.Item(1).ToString("yyyy-MM-dd"))) 'ISO1806format date.
xtwFeed.WriteElementString("changefreq","monthly") 'Or dr.Item(2).ToString
xtwFeed.WriteElementString("priority","0.5") 'Ordr.Item(3).ToString
xtwFeed.WriteEndElement()
End While
dr.Close()
con.Close()
xtwFeed.WriteEndElement()
xtwFeed.Flush()
xtwFeed.Close()
Response.End()
End Sub
End Class