Domanda

i have been searching around but I cannot seem to find any information on this issue.

if you go to https://support.google.com/webmasters/answer/34648 it says one must add the mobile tag to your xml for it to be crawled properly. My problem lies in that I have no idea on how to actually make this tag when using XDocument.

Does anyone know how to acutally write this tag

<mobile:mobile/>

using the XElement tag?

I have the following code that generates a document

XNamespace sitemap = XNamespace.Get("http://www.sitemaps.org/schemas/sitemap/0.9");
XNamespace mobile = XNamespace.Get("http://www.google.com/schemas/sitemap-mobile/1.0");

XDocument doc = new XDocument(
    new XDeclaration("1.0", "utf-8", null),
    new XElement(sitemap + "urlset",
      new XAttribute(XNamespace.Xmlns + "mobile", mobile))
);

and the following code that builds an element

private XElement BuildSitemapItem(XNamespace ns)
{
    XElement urlNode = new XElement(ns + "url",
        new XElement(ns +"loc"),
        new XElement(ns + "lastmod")
    );

    return urlNode;
}

I have been stuck on this issue for a while so any help would be appreciated.

È stato utile?

Soluzione

You just need to specify the right namespace on your XElement (in this case mobile)

XNamespace mobileNs = "http://www.google.com/schemas/sitemap-mobile/1.0";
new XElement(mobileNs + "mobile")

That will output <mobile:mobile/>

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top