Web design and hosting, database, cloud and social media solutions that deliver business results
  • 商务解决方案
  • 数据库咨询服务
    • 报告
      • Claytabase Server Disk IOPs Calculator
      • SQL代码备份
      • SQL打印机
    • 技术文章
      • SQL-Server
      • SQL Server 2008的维护计划
      • 使用SQL Server函数
      • 使用SQL Server日期
      • 使用SQL Server透视-取消透视
  • 网站设计
    • 怀特岛
    • 技术文章
      • ASP-NET
      • CSS
    • 网站安全
  • 产品展示
  • 社交媒体
  • 关于我们
    • 投资组合
    • 球队
      • 切斯特Copperpot
  • 学院
čeština (CS)Deutsch (DE)English (EN-US)English (EN-GB)Español (ES)Français (FR)हिंदी (HI)italiano (IT)日本語 (JA)polski (PL)Português (PT)русский (RU)Türk (TR)中国的 (ZH)

在 ASP NET 中从 SQL Server 创建 XML 站点地图

在 ASP NET 中从 SQL Server 创建 XML 站点地图

关于

在设计我们的内容管理系统的过程中,我们知道需要包含一个站点地图文件,以使搜索引擎使用的各种爬虫能够快速发现内容。

由于我们的系统背后已经有一个数据库,显而易见的选择是使用这些数据并动态创建文件。

系统需要能够处理大量页面,因此为了内置冗余,我们创建了一个站点地图索引页面,然后链接到站点地图。

XML Sitemap Index

<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

  <sitemap>   

  <loc>https://www.claytabase.co.uk/sitemap.xml?Language=EN&Page=1</loc>

  <lastmod>2019-07-17</lastmod>

</sitemap>

<sitemap>

  <loc>https://www.claytabase.co.uk/sitemap.xml?Language=EN&Page=2</loc>

  <lastmod>2019-07-17</lastmod>

</sitemap>

</sitemapindex>

XML Sitemap

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

  <url>

    <loc>https://www.claytabase.co.uk/</loc>

    <lastmod>2013-12-13</lastmod>

    <changefreq>monthly</changefreq>

    <priority>1</priority>

  </url>

  <url>

    <loc>https://www.claytabase.co.uk/About-Us</loc>

    <lastmod>2013-12-09</lastmod>

    <changefreq>monthly</changefreq>

    <priority>1</priority>

  </url>

</urlset>

如何生成它

为了便于阅读,我将代码生成分为两个类,但如果您愿意,可以将其组合起来。

生成与页面加载事件挂钩,但相当简单。

  • 设置响应内容类型为 text/xml 编码为 utf-8
  • 创建一个 XML 文本编写器并开始编写文档
  • 使用保存在 Web 配置文件中的连接字符串创建 SQL 连接
  • 从数据库中获取所有可能的值并将它们推送到数据集中以供读取
  • 编写第一个元素(sitemapindex 或 urlset),这是标准的一部分,并设置架构
  • 开始循环遍历数据集
    • 编写 require 元素(站点地图或 url)
    • 为每个元素编写所需的属性
  • 清理并关闭流

Sitemap Index

Imports System.Data

Imports System.Data.SqlClient

Imports System.Xml

Partial Class Generate_SitemapIndex

   Inherits System.Web.UI.Page

   Dim conStr As String = ConfigurationManager.ConnectionStrings("MySqlConnection").ConnectionString

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

       Response.Clear()

       Response.ContentType = "text/xml"

       Response.Charset = "Utf-8"

       Dim xtwFeed As XmlTextWriter = New XmlTextWriter(Response.OutputStream, Encoding.UTF8)

       xtwFeed.WriteStartDocument()

       Using con As New SqlConnection(conStr)

           Dim com As New SqlCommand("SELECT {SitemapUrl},{SitemapModified} FROM {YourDatabase}", con)

           Dim ds As New DataSet, da As New SqlDataAdapter(com)

           con.Open()

           da.Fill(ds)

           xtwFeed.WriteStartElement("sitemapindex")

           xtwFeed.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9")

           Dim dr = ds.Tables(0).CreateDataReader

           While dr.Read

               xtwFeed.WriteStartElement("sitemap")

               xtwFeed.WriteElementString("loc", dr.Item(0).ToString) 'OR full URL from your database!

               xtwFeed.WriteElementString("lastmod", dr.Item(1).ToString) 'ISO1806 format date.

               xtwFeed.WriteEndElement()

           End While

           xtwFeed.WriteEndElement()

       End Using

       xtwFeed.WriteEndDocument()

       xtwFeed.Flush()

       xtwFeed.Close()

       Response.End()

   End Sub

End Class

Sitemap

Imports System.Data

Imports System.Data.SqlClient

Imports System.Xml


Partial Class Generate_Sitemap

   Inherits System.Web.UI.Page

   Dim conStr As String = ConfigurationManager.ConnectionStrings("MySqlConnection").ConnectionString

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

       Response.Clear()

       Response.ContentType = "text/xml"

       Response.Charset = "Utf-8"

       Dim xtwFeed As XmlTextWriter = New XmlTextWriter(Response.OutputStream, Encoding.UTF8)

       xtwFeed.WriteStartDocument()

       Using con As New SqlConnection(conStr)

           Dim com As New SqlCommand("SELECT {PageUrl},{PageModified},{PageChangeFreq},{PagePriority} FROM {YourDatabase}", con)

           Dim ds As New DataSet, da As New SqlDataAdapter(com)

           con.Open()

           da.Fill(ds)

           xtwFeed.WriteStartElement("urlset")

           xtwFeed.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9")

           Dim dr = ds.Tables(0).CreateDataReader

           While dr.Read

               xtwFeed.WriteStartElement("url")

               xtwFeed.WriteElementString("loc", dr.Item(0).ToString) 'full URL from your database!

               xtwFeed.WriteElementString("lastmod", dr.Item(1).ToString) 'ISO1806 format date.

               xtwFeed.WriteElementString("changefreq", dr.Item(2).ToString) 'daily, weekly, monthly etc

               xtwFeed.WriteElementString("priority", dr.Item(3).ToString) 0.0 to 1.0

               xtwFeed.WriteEndElement()

           End While

           xtwFeed.WriteEndElement()

       End Using

       xtwFeed.WriteEndDocument()

       xtwFeed.Flush()

       xtwFeed.Close()

       Response.End()

   End Sub

End Class

Author

Helpful?

Please note, this commenting system is still in final testing.

Claytabase 网站设计

这是从 Ousia 内容管理系统代码修改而来的一段代码,它是市场上最快和最优化的系统之一,是我们网站设计服务的一部分。

更多:响应迅速。使用内容管理系统进行 Web 开发、设计和托管
Copyright Claytabase Ltd 2020

Registered in England and Wales 08985867

RSSLoginLink Cookie政策网站地图

Social Media

facebook.com/Claytabaseinstagram.com/claytabase/twitter.com/Claytabaselinkedin.com/company/claytabase-ltd

Get in Touch

+442392064871info@claytabase.comClaytabase Ltd, Unit 3d, Rink Road Industrial Estate, PO33 2LT, United Kingdom
此网站上的设置设置为允许所有Cookie。 这些可以在我们的Cookie政策和设置页面上更改。继续使用本网站即表示您同意使用Cookie。
Ousia Logo
Logout
Ousia CMS Loader