这两天给自己网站写了提交sitemap的代码,如果是新手,请过来看,如果是高手,可以不用看。
aspx代码
Code
1 <tr>
2 <td class="detailright" style="150px;">
3 生成www开头的课程:
4 </td>
5 <td class="detailcontent">
6 <asp:TextBox runat="server" ID="txtWWWCourseStart" />
7 -
8 <asp:TextBox runat="server" ID="txtWWWCourseEnd" />
9 <asp:Button ID="btnWWWCourse" Text="生成课程XML" runat="server" OnClick="btnWWWCourse_Click" OnClientClick="return CheckWWWCourse();" />
10 [生成的www1-5.xml文件,表示是 http://www.lesson9.com/course/100.html的方式访问课程。]
11 </td>
12 </tr>
1 <tr>
2 <td class="detailright" style="150px;">
3 生成www开头的课程:
4 </td>
5 <td class="detailcontent">
6 <asp:TextBox runat="server" ID="txtWWWCourseStart" />
7 -
8 <asp:TextBox runat="server" ID="txtWWWCourseEnd" />
9 <asp:Button ID="btnWWWCourse" Text="生成课程XML" runat="server" OnClick="btnWWWCourse_Click" OnClientClick="return CheckWWWCourse();" />
10 [生成的www1-5.xml文件,表示是 http://www.lesson9.com/course/100.html的方式访问课程。]
11 </td>
12 </tr>
cs代码:
Code
protected void btnWWWCourse_Click(object sender, EventArgs e)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineOnAttributes = true;
try
{
int start = Convert.ToInt32(this.txtWWWCourseStart.Text.Trim());
int end = Convert.ToInt32(this.txtWWWCourseEnd.Text.Trim());
XmlTextWriter writer1 = new XmlTextWriter(@"X:\www\www" + start.ToString() + "-" + end.ToString() + ".xml", System.Text.Encoding.UTF8);
XmlWriter writer = XmlWriter.Create(writer1, settings);
writer.WriteStartDocument();
writer.WriteStartElement("urlset");
writer.WriteAttributeString("xmlns", "http://www.google.com/schemas/sitemap/0.84");
var r = from i in db.Courses where i.Id <= end && i.Id >= start orderby i.Id ascending select i;
if (r.Count() > 0)
{
foreach (Lesson9.Entity.Course course in r.ToList())
{
writer.WriteStartElement("url");
writer.WriteElementString("loc", "http://www.XXX.com/course/" + course.Id + ".html");
string tempMonth = DateTime.Now.Month.ToString();
if (tempMonth.Length == 1)
{
tempMonth = "0" + tempMonth;
}
string tempDay = DateTime.Now.Day.ToString();
if (tempDay.Length == 1)
{
tempDay = "0" + tempDay;
}
writer.WriteElementString("lastmod", DateTime.Now.Year + "-" + tempMonth + "-" + tempDay);
writer.WriteElementString("changefreq", "monthly");
writer.WriteElementString("priority", "0.5");
writer.WriteEndElement();
}
}
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();
this.SetMessageInfo(spanMessage, "成功添加XML文件");
}
catch (Exception ex)
{
this.SetMessageInfo(spanMessage, "添加出错了:"+ ex.Message);
}
}
protected void btnWWWCourse_Click(object sender, EventArgs e)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineOnAttributes = true;
try
{
int start = Convert.ToInt32(this.txtWWWCourseStart.Text.Trim());
int end = Convert.ToInt32(this.txtWWWCourseEnd.Text.Trim());
XmlTextWriter writer1 = new XmlTextWriter(@"X:\www\www" + start.ToString() + "-" + end.ToString() + ".xml", System.Text.Encoding.UTF8);
XmlWriter writer = XmlWriter.Create(writer1, settings);
writer.WriteStartDocument();
writer.WriteStartElement("urlset");
writer.WriteAttributeString("xmlns", "http://www.google.com/schemas/sitemap/0.84");
var r = from i in db.Courses where i.Id <= end && i.Id >= start orderby i.Id ascending select i;
if (r.Count() > 0)
{
foreach (Lesson9.Entity.Course course in r.ToList())
{
writer.WriteStartElement("url");
writer.WriteElementString("loc", "http://www.XXX.com/course/" + course.Id + ".html");
string tempMonth = DateTime.Now.Month.ToString();
if (tempMonth.Length == 1)
{
tempMonth = "0" + tempMonth;
}
string tempDay = DateTime.Now.Day.ToString();
if (tempDay.Length == 1)
{
tempDay = "0" + tempDay;
}
writer.WriteElementString("lastmod", DateTime.Now.Year + "-" + tempMonth + "-" + tempDay);
writer.WriteElementString("changefreq", "monthly");
writer.WriteElementString("priority", "0.5");
writer.WriteEndElement();
}
}
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();
this.SetMessageInfo(spanMessage, "成功添加XML文件");
}
catch (Exception ex)
{
this.SetMessageInfo(spanMessage, "添加出错了:"+ ex.Message);
}
}
另1:XmlWriter writer = XmlWriter.Create(writer1, settings); 如果 writer1是直接文件路径如 d:\TestFolder\TestFile.xml 则settings有效果,不然无效。
哪位知其原,请告诉我。不甚感谢。
另2:google sitemap 暂不支持二级域名的提交,哪位有好方法,欢迎献上来。