0

我生成一个 sitemap.xml

XDocument xDoc = new XDocument(
    new XDeclaration("1.0", "UTF-8", ""),
    new XElement("urlset",
    new XAttribute("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9"),
    new Element (....and so on...)

我收到一个错误

前缀 '' 不能在同一起始元素标记中从 '' 重新定义为 ' http://www.sitemaps.org/schemas/sitemap/0.9 '。

Google 要求没有任何前缀的 xmlns 属性。

4

2 回答 2

0

您可以通过使用空白命名空间来解决此问题,如下所示:

    XNamespace blank = XNamespace.Get(@"http://www.sitemaps.org/schemas/sitemap/0.9");

    XDocument doc = new XDocument(
        new XDeclaration("1.0", "utf-8", "yes"),
        new XElement(blank + "urlset",
            new XAttribute("xmlns", blank.NamespaceName),  
            new XElement(blank + "loc", "http://www.abc.eba/"),               
            new Element (....and so on...)
         ));
于 2014-11-12T11:49:03.603 回答
0

似乎添加默认命名空间XDocument有点棘手,相关问题:How to set the default XML namespace for an XDocument

您可以尝试声明默认命名空间前缀并将该前缀用于其中的所有元素,<urlset>如下所示:

XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
XDocument xDoc = new XDocument(
    new XDeclaration("1.0", "UTF-8", ""),
    new XElement(ns+"urlset",
    new XElement(ns+"otherElement"),
    new XElement (....and so on...)
于 2014-08-13T12:56:33.027 回答