In the past two days, I wrote code for submitting sitemap to my website. If you are a newbie, please come and see it. If you are a master, you don't need to check it.
Aspx code
Code
1 <tr>
2 <td class = "detailright" style = "width: 150px;">
3. Generate a course starting with 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 = "generate course XML" runat = "server" OnClick = "btnWWWCourse_Click" OnClientClick = "return CheckWWWCourse ();"/>
10 [the generated www1-5.xml file, indicating that it is an official course schedule for http://www.lesson9.com/course/100.html]
11 </td>
12 </tr>
Cs code:
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.txt WWWCourseStart. Text. Trim ());
Int end = Convert.ToInt32(this.txt WWWCourseEnd. Text. Trim ());
XmlTextWriter writer1 = new XmlTextWriter (@ "X: \ 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 file added successfully ");
}
Catch (Exception ex)
{
This. SetMessageInfo (spanMessage, "error added:" + ex. Message );
}
}
Another 1: XmlWriter writer = XmlWriter. Create (writer1, settings); IfWriter1Is the direct file path such as d: \ TestFolder \ TestFile. xml, the settings effect, otherwise it is invalid.
If you know the original information, please let me know. Thank you very much.
Another 2: google sitemap does not support Submission of second-level domain names. Which of the following is a good method? Please submit it.