站点地图不显示为xml

时间:2021-08-02 20:25:31

I've got the following classes for my sitemap generation:

我的站点地图生成有以下类:

public class SitemapItem
{
    public SitemapItem(string url)
    {
        this.Url = url;
        this.AlternateLinks = new List<SiteMapAlternateLink>();
    }

    public string Url { get; set; }

    public DateTime? LastModified { get; set; }

    public ChangeFrequency? ChangeFrequency { get; set; }

    public float? Priority { get; set; }

    public List<SiteMapAlternateLink> AlternateLinks { get; set; }
}

And:

和:

public class SiteMapAlternateLink
{
    public SiteMapAlternateLink(string url, string language)
    {
        this.Url = url;
        this.Language = language;
    }

    public string Url { get; set; }

    public string Language { get; set; }
}

Now in my controller I fill a list of SitemapItems and return it from the controller with the following code:

现在在我的控制器中,我填写SitemapItems列表并使用以下代码从控制器返回它:

public class XmlSitemapResult : ActionResult
{
    private XNamespace nsSitemap = "http://www.sitemaps.org/schemas/sitemap/0.9";
    private XNamespace nsXhtml = "http://www.w3.org/1999/xhtml";

    private IEnumerable<SitemapItem> _items;

    public XmlSitemapResult(IEnumerable<SitemapItem> items)
    {
        _items = items;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        string encoding = context.HttpContext.Response.ContentEncoding.WebName;
        XDocument sitemap = new XDocument(new XDeclaration("1.0", encoding, "yes"),
             new XElement(nsSitemap + "urlset", new XAttribute(XNamespace.Xmlns + "xhtml", nsXhtml),
                  from item in _items
                  select CreateItemElement(item)
                  )
             );

        context.HttpContext.Response.ContentType = "application/xml";
        context.HttpContext.Response.Charset = encoding;
        context.HttpContext.Response.Flush();
        context.HttpContext.Response.Write(sitemap.Declaration + sitemap.ToString());
    }

    private XElement CreateItemElement(SitemapItem item)
    {
        XElement itemElement = new XElement(nsSitemap + "url", new XElement(nsSitemap + "loc", item.Url.ToLower()));

        if (item.LastModified.HasValue)
            itemElement.Add(new XElement(nsSitemap + "lastmod", item.LastModified.Value.ToString("yyyy-MM-dd")));

        if (item.ChangeFrequency.HasValue)
            itemElement.Add(new XElement(nsSitemap + "changefreq", item.ChangeFrequency.Value.ToString().ToLower()));

        if (item.Priority.HasValue)
            itemElement.Add(new XElement(nsSitemap + "priority", item.Priority.Value.ToString(CultureInfo.InvariantCulture)));

        foreach (var alternateLink in item.AlternateLinks)
        {
            itemElement.Add(new XElement(nsXhtml + "link", 
                new XAttribute("rel", "alternate"),
                new XAttribute("hreflang", alternateLink.Language),
                new XAttribute("href", alternateLink.Url)));
        }

        return itemElement;
    }
}

Now the problem is that in my browser I won't see the XML as XML. I'll just see text which doesn't get viewed with the standard XML Viewer. This happens in all browsers and it seems it happend from the moment I added the xhtml schema.

现在的问题是,在我的浏览器中,我不会将XML视为XML。我只会看到使用标准XML Viewer无法查看的文本。这种情况发生在所有浏览器中,似乎从我添加xhtml架构的那一刻起就发生了。

Hopefully somebody sees the problem, thanks in advance!

希望有人看到问题,提前谢谢!

EDIT: If I remove everything that has to do with xhtml the browser DOES show it as a xml. Any ideas?

编辑:如果我删除与xhtml有关的所有内容,浏览器会将其显示为xml。有任何想法吗?

EDIT2: The html:

EDIT2:html:

 <urlset xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>http://localhost:11149/en</loc>
    <changefreq>hourly</changefreq>
    <priority>0.6</priority>
    <xhtml:link rel="alternate" hreflang="en" href="http://localhost:11149/en"/>
    <xhtml:link rel="alternate" hreflang="nl" href="http://localhost:11149/nl"/>
  </url>
  <url>
    <loc>http://localhost:11149/en/buyandsell</loc>
    <changefreq>weekly</changefreq>
    <priority>1</priority>
    <xhtml:link rel="alternate" hreflang="en" href="http://localhost:11149/en/BuyAndSell"/>
    <xhtml:link rel="alternate" hreflang="nl" href="http://localhost:11149/nl/BuyAndSell"/>
  </url>
  <url>
    <loc>http://localhost:11149/en/partner</loc>
    <changefreq>weekly</changefreq>
    <priority>0.5</priority>
    <xhtml:link rel="alternate" hreflang="en" href="http://localhost:11149/en/Partner"/>
    <xhtml:link rel="alternate" hreflang="nl" href="http://localhost:11149/nl/Partner"/>
  </url>
  <url>
    <loc>http://localhost:11149/en/news</loc>
    <lastmod>2013-12-06</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.6</priority>
    <xhtml:link rel="alternate" hreflang="en" href="http://localhost:11149/en/News"/>
    <xhtml:link rel="alternate" hreflang="nl" href="http://localhost:11149/nl/News"/>
  </url>
</urlset>

2 个解决方案

#1


3  

Here's what we do to generate the sitemap for autoquoter.com. See http://www.autoquoter.com/aq/sitemap.xml

以下是我们为autoquoter.com生成站点地图的操作。请参见http://www.autoquoter.com/aq/sitemap.xml

        var sitemap = new XDocument(
            new XDeclaration("1.0", "utf-8", "yes"),
            new XProcessingInstruction("xml-stylesheet",
                "type=\"text/xsl\" href=\"" + Url.AbsoluteAction("SitemapXsl", "Default") + "\""),
            new XElement(ns + "urlset",
                new XAttribute(XNamespace.Xmlns + "sitemap", ns),
                new XAttribute(XNamespace.Xmlns + "xhtml", xhtml), 
                nodeList));

        Response.AddHeader("X-Robots-Tag","noindex");
        return Content(sitemap.Declaration+"\r\n"+sitemap, "text/xml");

We also use an xsl stylesheet to transform the sitemap. This helps with browsers that don't provide a decent formatter automatically. See http://www.autoquoter.com/aq/en/Default/SitemapXsl for the stylesheet that we are using.

我们还使用xsl样式表来转换站点地图。这有助于浏览器无法自动提供合适的格式化程序。有关我们正在使用的样式表,请参见http://www.autoquoter.com/aq/en/Default/SitemapXsl。

#2


1  

EDIT 1:

编辑1:

It seems that you may lose some of the xml specific header files:

您似乎可能会丢失一些特定于xml的头文件:

Check this link out

检查此链接

This might also be a valid example on how to create an xml site map:

这也可能是有关如何创建xml站点地图的有效示例:

Sitemap xml for google Another example
OPTION1 Try this code out(taken from here):

谷歌的Sitemap xml另一个例子OPTION1尝试这个代码(取自这里):

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Sitemap : IHttpHandler
    {
        private const string NAMESPACE = "http://www.sitemaps.org/schemas/sitemap/0.9";

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/xml";
            XmlDocument sitemapDocument = GetSitemapDocument();
            context.Response.Write(sitemapDocument.InnerXml);
        }

        #region Build sitemap document methods
        private XmlDocument GetSitemapDocument()
        {
            XmlDocument sitemapDocument = new XmlDocument();
            sitemapDocument.PreserveWhitespace = true;

            XmlDeclaration xmlDeclaration = 
                sitemapDocument.CreateXmlDeclaration("1.0", "UTF-8", string.Empty);
            sitemapDocument.AppendChild(xmlDeclaration);

            XmlElement urlset = sitemapDocument.CreateElement("urlset", NAMESPACE);
            sitemapDocument.AppendChild(urlset);

            List<SitemapPage> urls = GetSitemapPages();

            foreach (SitemapPage sitemapPage in urls)
            {
                XmlElement url = CreateUrlElement(sitemapDocument, sitemapPage);
                urlset.AppendChild(url);
            }
            return sitemapDocument;
        }

        private XmlElement CreateUrlElement(XmlDocument sitemapDocument, 
            SitemapPage sitemapPage)
        {
            XmlElement url = sitemapDocument.CreateElement("url", NAMESPACE);

            XmlElement loc = CreateElementWithText(sitemapDocument, "loc", 
                sitemapPage.Location);
            url.AppendChild(loc);

            if (sitemapPage.LastModificationDate.HasValue)
            {
                //lastmod must be a string that comforms to the W3C Datetime format
                string lastModValue = sitemapPage.LastModificationDate.Value.ToString(
                    "yyyy'-'MM'-'dd'T'HH':'mm':'ssK");
                XmlElement lastmod = CreateElementWithText(
                    sitemapDocument, "lastmod", lastModValue);
                url.AppendChild(lastmod);
            }

            if (!string.IsNullOrEmpty(sitemapPage.ChangeFrequency))
            {
                XmlElement changefreq = CreateElementWithText(sitemapDocument, 
                    "changefreq", sitemapPage.ChangeFrequency);
                url.AppendChild(changefreq);
            }

            if (sitemapPage.Priority.HasValue)
            {
                XmlElement priority = CreateElementWithText(sitemapDocument,
                    "priority", sitemapPage.Priority.Value.ToString(
                        CultureInfo.CreateSpecificCulture("en-US")));
                url.AppendChild(priority);
            }

            return url;
        }

        private XmlElement CreateElementWithText(
            XmlDocument document, string elementName, string text)
        {
            XmlElement element = document.CreateElement(elementName, NAMESPACE);

            XmlText elementValue = document.CreateTextNode(text);
            element.AppendChild(elementValue);

            return element;
        }
        #endregion

        private List<SitemapPage> GetSitemapPages()
        {
            List<SitemapPage> sitemapPages = new List<SitemapPage>();

            //Example implementation
            sitemapPages.Add(new SitemapPage("http://www.mydomain.com") 
                { ChangeFrequency = "daily", LastModificationDate = DateTime.Now, Priority = 1f });
            sitemapPages.Add(new SitemapPage("http://www.mydomain.com/aPage.aspx") 
                { ChangeFrequency = "daily", LastModificationDate = DateTime.Now, Priority = 0.8f });

            return sitemapPages;
        }

        private class SitemapPage
        {
            public SitemapPage(string location)
            {
                Location = location;
            }

            public string Location { get; private set; }
            public DateTime? LastModificationDate { get; set; }
            public string ChangeFrequency { get; set; }
            public float? Priority { get; set; }
        }

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
    }

OPTION2: According to MSDN this is a valid sitemap: sitemap MSDN.

选项2:根据MSDN,这是一个有效的站点地图:站点地图MSDN。

Check this output out:

检查此输出:

<siteMap>
  <siteMapNode title="Home" description="Home" url="~/default.aspx">
    <siteMapNode title="Products" description="Our products"
      url="~/Products.aspx">
      <siteMapNode title="Hardware" description="Hardware choices"
        url="~/Hardware.aspx" />
      <siteMapNode title="Software" description="Software choices"
        url="~/Software.aspx" />
    </siteMapNode>
    <siteMapNode title="Services" description="Services we offer"
        url="~/Services.aspx">
        <siteMapNode title="Training" description="Training classes"
          url="~/Training.aspx" />
        <siteMapNode title="Consulting" description="Consulting services" 
          url="~/Consulting.aspx" />
        <siteMapNode title="Support" description="Supports plans" 
          url="~/Support.aspx" />
    </siteMapNode>
  </siteMapNode>
</siteMap>

Maybe you should try change your code:

也许您应该尝试更改您的代码:

public class XmlSitemapResult : ActionResult
{
    private XNamespace nsSitemap = "http://www.sitemaps.org/schemas/sitemap/0.9";
    private XNamespace nsXhtml = "http://www.w3.org/1999/xhtml";

    private IEnumerable<SitemapItem> _items;

    public XmlSitemapResult(IEnumerable<SitemapItem> items)
    {
        _items = items;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        string encoding = context.HttpContext.Response.ContentEncoding.WebName;
        XDocument sitemap = new XDocument(new XDeclaration("1.0", encoding, "yes"),
             new XElement(nsSitemap + "siteMap", new XAttribute(XNamespace.Xmlns + "xhtml", nsXhtml),
                  from item in _items
                  select CreateItemElement(item)
                  )
             );

        context.HttpContext.Response.ContentType = "application/xml";
        context.HttpContext.Response.Charset = encoding;
        context.HttpContext.Response.Flush();
        context.HttpContext.Response.Write(sitemap.Declaration + sitemap.ToString());
    }

    private XElement CreateItemElement(SitemapItem item)
    {
        XElement itemElement = new XElement(nsSitemap + "siteMapNode", new XElement(nsSitemap + "loc", item.Url.ToLower()));

        if (item.LastModified.HasValue)
            itemElement.Add(new XElement(nsSitemap + "lastmod", item.LastModified.Value.ToString("yyyy-MM-dd")));

        if (item.ChangeFrequency.HasValue)
            itemElement.Add(new XElement(nsSitemap + "changefreq", item.ChangeFrequency.Value.ToString().ToLower()));

        if (item.Priority.HasValue)
            itemElement.Add(new XElement(nsSitemap + "priority", item.Priority.Value.ToString(CultureInfo.InvariantCulture)));

        foreach (var alternateLink in item.AlternateLinks)
        {
            itemElement.Add(new XElement(nsXhtml + "link", 
                new XAttribute("rel", "alternate"),
                new XAttribute("hreflang", alternateLink.Language),
                new XAttribute("href", alternateLink.Url)));
        }

        return itemElement;
    }
}

#1


3  

Here's what we do to generate the sitemap for autoquoter.com. See http://www.autoquoter.com/aq/sitemap.xml

以下是我们为autoquoter.com生成站点地图的操作。请参见http://www.autoquoter.com/aq/sitemap.xml

        var sitemap = new XDocument(
            new XDeclaration("1.0", "utf-8", "yes"),
            new XProcessingInstruction("xml-stylesheet",
                "type=\"text/xsl\" href=\"" + Url.AbsoluteAction("SitemapXsl", "Default") + "\""),
            new XElement(ns + "urlset",
                new XAttribute(XNamespace.Xmlns + "sitemap", ns),
                new XAttribute(XNamespace.Xmlns + "xhtml", xhtml), 
                nodeList));

        Response.AddHeader("X-Robots-Tag","noindex");
        return Content(sitemap.Declaration+"\r\n"+sitemap, "text/xml");

We also use an xsl stylesheet to transform the sitemap. This helps with browsers that don't provide a decent formatter automatically. See http://www.autoquoter.com/aq/en/Default/SitemapXsl for the stylesheet that we are using.

我们还使用xsl样式表来转换站点地图。这有助于浏览器无法自动提供合适的格式化程序。有关我们正在使用的样式表,请参见http://www.autoquoter.com/aq/en/Default/SitemapXsl。

#2


1  

EDIT 1:

编辑1:

It seems that you may lose some of the xml specific header files:

您似乎可能会丢失一些特定于xml的头文件:

Check this link out

检查此链接

This might also be a valid example on how to create an xml site map:

这也可能是有关如何创建xml站点地图的有效示例:

Sitemap xml for google Another example
OPTION1 Try this code out(taken from here):

谷歌的Sitemap xml另一个例子OPTION1尝试这个代码(取自这里):

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Sitemap : IHttpHandler
    {
        private const string NAMESPACE = "http://www.sitemaps.org/schemas/sitemap/0.9";

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/xml";
            XmlDocument sitemapDocument = GetSitemapDocument();
            context.Response.Write(sitemapDocument.InnerXml);
        }

        #region Build sitemap document methods
        private XmlDocument GetSitemapDocument()
        {
            XmlDocument sitemapDocument = new XmlDocument();
            sitemapDocument.PreserveWhitespace = true;

            XmlDeclaration xmlDeclaration = 
                sitemapDocument.CreateXmlDeclaration("1.0", "UTF-8", string.Empty);
            sitemapDocument.AppendChild(xmlDeclaration);

            XmlElement urlset = sitemapDocument.CreateElement("urlset", NAMESPACE);
            sitemapDocument.AppendChild(urlset);

            List<SitemapPage> urls = GetSitemapPages();

            foreach (SitemapPage sitemapPage in urls)
            {
                XmlElement url = CreateUrlElement(sitemapDocument, sitemapPage);
                urlset.AppendChild(url);
            }
            return sitemapDocument;
        }

        private XmlElement CreateUrlElement(XmlDocument sitemapDocument, 
            SitemapPage sitemapPage)
        {
            XmlElement url = sitemapDocument.CreateElement("url", NAMESPACE);

            XmlElement loc = CreateElementWithText(sitemapDocument, "loc", 
                sitemapPage.Location);
            url.AppendChild(loc);

            if (sitemapPage.LastModificationDate.HasValue)
            {
                //lastmod must be a string that comforms to the W3C Datetime format
                string lastModValue = sitemapPage.LastModificationDate.Value.ToString(
                    "yyyy'-'MM'-'dd'T'HH':'mm':'ssK");
                XmlElement lastmod = CreateElementWithText(
                    sitemapDocument, "lastmod", lastModValue);
                url.AppendChild(lastmod);
            }

            if (!string.IsNullOrEmpty(sitemapPage.ChangeFrequency))
            {
                XmlElement changefreq = CreateElementWithText(sitemapDocument, 
                    "changefreq", sitemapPage.ChangeFrequency);
                url.AppendChild(changefreq);
            }

            if (sitemapPage.Priority.HasValue)
            {
                XmlElement priority = CreateElementWithText(sitemapDocument,
                    "priority", sitemapPage.Priority.Value.ToString(
                        CultureInfo.CreateSpecificCulture("en-US")));
                url.AppendChild(priority);
            }

            return url;
        }

        private XmlElement CreateElementWithText(
            XmlDocument document, string elementName, string text)
        {
            XmlElement element = document.CreateElement(elementName, NAMESPACE);

            XmlText elementValue = document.CreateTextNode(text);
            element.AppendChild(elementValue);

            return element;
        }
        #endregion

        private List<SitemapPage> GetSitemapPages()
        {
            List<SitemapPage> sitemapPages = new List<SitemapPage>();

            //Example implementation
            sitemapPages.Add(new SitemapPage("http://www.mydomain.com") 
                { ChangeFrequency = "daily", LastModificationDate = DateTime.Now, Priority = 1f });
            sitemapPages.Add(new SitemapPage("http://www.mydomain.com/aPage.aspx") 
                { ChangeFrequency = "daily", LastModificationDate = DateTime.Now, Priority = 0.8f });

            return sitemapPages;
        }

        private class SitemapPage
        {
            public SitemapPage(string location)
            {
                Location = location;
            }

            public string Location { get; private set; }
            public DateTime? LastModificationDate { get; set; }
            public string ChangeFrequency { get; set; }
            public float? Priority { get; set; }
        }

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
    }

OPTION2: According to MSDN this is a valid sitemap: sitemap MSDN.

选项2:根据MSDN,这是一个有效的站点地图:站点地图MSDN。

Check this output out:

检查此输出:

<siteMap>
  <siteMapNode title="Home" description="Home" url="~/default.aspx">
    <siteMapNode title="Products" description="Our products"
      url="~/Products.aspx">
      <siteMapNode title="Hardware" description="Hardware choices"
        url="~/Hardware.aspx" />
      <siteMapNode title="Software" description="Software choices"
        url="~/Software.aspx" />
    </siteMapNode>
    <siteMapNode title="Services" description="Services we offer"
        url="~/Services.aspx">
        <siteMapNode title="Training" description="Training classes"
          url="~/Training.aspx" />
        <siteMapNode title="Consulting" description="Consulting services" 
          url="~/Consulting.aspx" />
        <siteMapNode title="Support" description="Supports plans" 
          url="~/Support.aspx" />
    </siteMapNode>
  </siteMapNode>
</siteMap>

Maybe you should try change your code:

也许您应该尝试更改您的代码:

public class XmlSitemapResult : ActionResult
{
    private XNamespace nsSitemap = "http://www.sitemaps.org/schemas/sitemap/0.9";
    private XNamespace nsXhtml = "http://www.w3.org/1999/xhtml";

    private IEnumerable<SitemapItem> _items;

    public XmlSitemapResult(IEnumerable<SitemapItem> items)
    {
        _items = items;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        string encoding = context.HttpContext.Response.ContentEncoding.WebName;
        XDocument sitemap = new XDocument(new XDeclaration("1.0", encoding, "yes"),
             new XElement(nsSitemap + "siteMap", new XAttribute(XNamespace.Xmlns + "xhtml", nsXhtml),
                  from item in _items
                  select CreateItemElement(item)
                  )
             );

        context.HttpContext.Response.ContentType = "application/xml";
        context.HttpContext.Response.Charset = encoding;
        context.HttpContext.Response.Flush();
        context.HttpContext.Response.Write(sitemap.Declaration + sitemap.ToString());
    }

    private XElement CreateItemElement(SitemapItem item)
    {
        XElement itemElement = new XElement(nsSitemap + "siteMapNode", new XElement(nsSitemap + "loc", item.Url.ToLower()));

        if (item.LastModified.HasValue)
            itemElement.Add(new XElement(nsSitemap + "lastmod", item.LastModified.Value.ToString("yyyy-MM-dd")));

        if (item.ChangeFrequency.HasValue)
            itemElement.Add(new XElement(nsSitemap + "changefreq", item.ChangeFrequency.Value.ToString().ToLower()));

        if (item.Priority.HasValue)
            itemElement.Add(new XElement(nsSitemap + "priority", item.Priority.Value.ToString(CultureInfo.InvariantCulture)));

        foreach (var alternateLink in item.AlternateLinks)
        {
            itemElement.Add(new XElement(nsXhtml + "link", 
                new XAttribute("rel", "alternate"),
                new XAttribute("hreflang", alternateLink.Language),
                new XAttribute("href", alternateLink.Url)));
        }

        return itemElement;
    }
}