c# - 站点地图不显示为 xml

标签 c# asp.net xml asp.net-mvc sitemap

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

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; }
}

和:

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; }
}

现在在我的 Controller 中,我填写了一个 SitemapItems 列表,并使用以下代码从 Controller 返回它:

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;
    }
}

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

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

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

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>

最佳答案

以下是我们为 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");

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

关于c# - 站点地图不显示为 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20427088/

相关文章:

c# - 保存工作簿时出错 : System. Runtime.InteropServices.COMException:HRESULT 异常:0x800A03EC

c# - 列表的 XML 序列化变得太大

java - XMLBeans - 处理命名空间

css - 使用外部 CSS 显示 XSLT 的结果

c# - 可能的 NullReferenceException

c# - 用表达式编写自己的断言函数

c# - 将复杂的json反序列化为类似字典的结构

c# - 如何在 .NET DLL 中嵌入 SQLite 数据库,然后从 C# 中使用它?

javascript - Jquery脚本删除表单集合中的所有项目

asp.net - 表单例份验证和身份验证票证 cookie 域