Omi
Omi

Reputation: 447

How to generate dynamic sitemap in asp.net?

I am trying to build up a sitemap for all my dynamic pages. I have tried generating it by using web form and writing code behind function. But on submitting the page on Google Webmaster it is giving me an error- 1. We encountered an error while trying to access your sitemap. Please ensure your sitemap follows our guidelines and can be accessed at the location you provided and then resubmit. 2. Sitemap is HTML- Your sitemap appears to be an HTML page. please use a supported sitemap format instead. This what I was trying-

protected void Page_Load(object sender, EventArgs e)
    {
        Response.Clear();
        Response.ContentType = "text/xml";
        using (XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Encoding.UTF8))
        {
            writer.WriteStartDocument();
            writer.WriteStartElement("urlset");
            writer.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");
            writer.WriteStartElement("url");
            writer.WriteElementString("loc","http://www.mywebsite.com/");
            writer.WriteElementString("changefreq","weekly");
            writer.WriteElementString("priority","1.0");
            writer.WriteEndElement();

            string connect = WebConfigurationManager.ConnectionStrings["connectionName"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(connect))
            {
                using(SqlCommand cmd1=new SqlCommand("select attr1,attr2,substring(attr3,0,300) as attr31, attr4 from tblname order by attr1",conn))
                {
                    cmd1.CommandType=CommandType.Text;
                    conn.Open();
                    using (SqlDataReader rdr1=cmd1.ExecuteReader())
                    {
                        while(rdr1.Read())
                        {
                            writer.WriteElementString("loc","http://www.mywebsite.com/page1.aspx");
                            if (rdr1[1] != DBNull.Value)
                                writer.WriteElementString("lastmod",String.Format("{0:yyyy-MM-dd}",rdr1[1]));
                            writer.WriteElementString("changefreq","daily");
                            writer.WriteElementString("priority","0.80");
                            writer.WriteEndElement();
                        }
                        rdr1.NextResult();
                        while (rdr1.Read())
                        {
                            writer.WriteElementString("loc","http://www.mywebsite.com/page2.aspx?id="+rdr1[0].ToString());
                            if (rdr1[1] != DBNull.Value)
                                writer.WriteElementString("lastmod",String.Format("{0:yyyy-MM-dd}",rdr1[1]));
                            writer.WriteElementString("changefreq","daily");
                            writer.WriteElementString("priority","0.80");
                            writer.WriteEndElement();
                        }
                        writer.WriteEndElement();
                        writer.WriteEndDocument();
                        writer.Flush();
                    }
                    Response.End();
                }
            }
        }
    }

And my aspx page is-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="sitemap.aspx.cs" Inherits="sitemap" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    </div>
    </form>
</body>
</html>

Please guide me where I am doing wrong.?

Upvotes: 1

Views: 2692

Answers (1)

Jan B&#252;hler
Jan B&#252;hler

Reputation: 421

Your aspx page clearly states <html …> as first element.

I think you need to delete everything in your aspx file except line 1. You need to make sure that the sitemap is reachable at domain/sitemap.xml.

In case you need further help, please download and provide the sitemap.xml file.

Upvotes: 1

Related Questions