我的博客

asp.net 自学笔记及开发过程中的经验、技巧、摘录
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

对GOOGLE sitemap.xml网络地图的添加for asp.net1.1

Posted on 2008-04-09 18:26  Net_Learner  阅读(595)  评论(0编辑  收藏  举报
sitemap.xml格式如下
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
  
<url>
    
<loc>http://www.ds168.com/</loc>
    
<priority>0.5</priority>
    
<lastmod>2008-03-11T08:45:45+00:00</lastmod>
    
<changefreq>always</changefreq>
  
</url>
  
<url>
    
<loc>http://www.ds168.com/ipr/</loc>
    
<priority>0.5</priority>
    
<lastmod>2008-04-09T06:11+08:00</lastmod>
    
<changefreq>always</changefreq>
  
</url>
  
<url>
    
<loc>http://www.ds168.com/bbs/</loc>
    
<priority>0.5</priority>
    
<lastmod>2008-04-09T06:11+08:00</lastmod>
    
<changefreq>always</changefreq>
  
</url>
</urlset>

思路是每添加一篇新的文章就会在这个里添加一条数据,以便GOOGLE收录,但做了几天一直出现问题

前台代码
<%@ Page language="c#" Codebehind="InsertXML.aspx.cs" AutoEventWireup="false" Inherits="TheTestXml.xml.InsertXML" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    
<HEAD>
        
<title>InsertXML</title>
        
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
        
<meta content="C#" name="CODE_LANGUAGE">
        
<meta content="JavaScript" name="vs_defaultClientScript">
        
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    
</HEAD>
    
<body MS_POSITIONING="GridLayout">
        
<form id="Form1" method="post" runat="server">
            
<asp:textbox id="tbUrl" style="Z-INDEX: 100; LEFT: 328px; POSITION: absolute; TOP: 80px" runat="server"></asp:textbox>
            
<DIV style="DISPLAY: inline; Z-INDEX: 107; LEFT: 216px; WIDTH: 70px; POSITION: absolute; TOP: 168px; HEIGHT: 15px"
                ms_positioning
="FlowLayout">更新频率</DIV>
            
<DIV style="DISPLAY: inline; Z-INDEX: 106; LEFT: 216px; WIDTH: 70px; POSITION: absolute; TOP: 128px; HEIGHT: 15px"
                ms_positioning
="FlowLayout">优先权</DIV>
            
<asp:textbox id="tbYx" style="Z-INDEX: 101; LEFT: 328px; POSITION: absolute; TOP: 120px" runat="server"></asp:textbox><asp:button id="btSave" style="Z-INDEX: 102; LEFT: 336px; POSITION: absolute; TOP: 208px" runat="server"
                Text
="添加"></asp:button><asp:dropdownlist id="DropDownList1" style="Z-INDEX: 103; LEFT: 328px; POSITION: absolute; TOP: 160px"
                runat
="server">
                
<asp:ListItem Value="always" Selected="True">always</asp:ListItem>
                
<asp:ListItem Value="hourly">hourly</asp:ListItem>
                
<asp:ListItem Value="daily">daily</asp:ListItem>
                
<asp:ListItem Value="weekly">weekly</asp:ListItem>
                
<asp:ListItem Value="monthly">monthly</asp:ListItem>
                
<asp:ListItem Value="yearly">yearly</asp:ListItem>
            
</asp:dropdownlist>
            
<DIV style="DISPLAY: inline; Z-INDEX: 105; LEFT: 208px; WIDTH: 70px; POSITION: absolute; TOP: 80px; HEIGHT: 15px"
                ms_positioning
="FlowLayout">网址</DIV>
        
</form>
    
</body>
</HTML>


后台代码

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Xml.Schema;
namespace TheTestXml.DsSiteAdmin.xml
{
    
/// <summary>
    
/// InsertXML 的摘要说明。
    
/// </summary>

    public class InsertXML : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.DropDownList DropDownList1;
        
protected System.Web.UI.WebControls.Button btSave;
        
protected System.Web.UI.WebControls.TextBox tbYx;
        
protected System.Web.UI.WebControls.TextBox tbUrl;
    
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面
        }


        
/// <summary>
        
/// 插入SITEMAP数据
        
/// </summary>
        
/// <param name="_webUrl"></param>
        
/// <param name="_priority"></param>
        
/// <param name="lastTime"></param>
        
/// <param name="freq"></param>

        private void AddXML(string _webUrl,string _priority,string lastTime,string freq)
        
{

            XmlNamespaceManager   nsmgr;       
            
string   path   =   Server.MapPath("~\\sitemap\\sitemap.xml");      
            XmlDocument   xmldoc   
=   new   XmlDocument();   
            xmldoc.Load(path);          
            nsmgr   
=   new   XmlNamespaceManager(xmldoc.NameTable); 
  
            nsmgr.AddNamespace(
"tk","http://www.google.com/schemas/sitemap/0.84");    

            XmlNode root
=xmldoc.SelectSingleNode("//tk:urlset",nsmgr);

            XmlElement xe1
=xmldoc.CreateElement("""url",xmldoc.DocumentElement.NamespaceURI);//创建一个<url>节点        
            
            XmlElement xesub1
=xmldoc.CreateElement("","loc",xmldoc.DocumentElement.NamespaceURI);        
            xesub1.InnerText
=_webUrl;//设置文本节点
            xe1.AppendChild(xesub1);//添加到<loc>节点中

            XmlElement xesub2
=xmldoc.CreateElement("","priority",xmldoc.DocumentElement.NamespaceURI);
            xesub2.InnerText
=_priority;
            xe1.AppendChild(xesub2);

            XmlElement xesub3
=xmldoc.CreateElement("","lastmod",xmldoc.DocumentElement.NamespaceURI);
            xesub3.InnerText
=lastTime;
            xe1.AppendChild(xesub3);

            XmlElement xesub4
=xmldoc.CreateElement("","changefreq",xmldoc.DocumentElement.NamespaceURI);
            xesub4.InnerText
=freq;
            xe1.AppendChild(xesub4);

            root.AppendChild(xe1);
//添加到<urlset>节点中    

            xmldoc.Save(Server.MapPath(
"~\\sitemap\\sitemap.xml"));    
        }

        
Web 窗体设计器生成的代码

        
private void btSave_Click(object sender, System.EventArgs e)
        
{
            
string dTime=string.Format("{0:yyyy-MM-ddThh:mmzzz}",DateTime.Now);
            AddXML(tbUrl.Text.Trim(),tbYx.Text.Trim(),dTime,DropDownList1.SelectedValue);
            
        }

    }

}