2

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.

John Conde
  • 217,595
  • 99
  • 455
  • 496
  • Have you tried simply adding a `new XElement(ns + "mobile")` inside of your `urlNode` constructor? While it isn't visually the same as ``, it would mean the same thing. The `mobile:` prefix is simply a short-hand namespace reference. – Sven Grosen Dec 17 '13 at 14:46
  • If you add this, it adds it to the xmlns="" I dont know if it still is the same then, I would prefer it looked exactly the same tho, unless someone can verify that google doesn't care how the tag looks like. But thanks. – Nicki Sibbern Dec 17 '13 at 15:10
  • yes, it will add the `xmlns` but that shouldn't matter; it should still work the same. – Sven Grosen Dec 17 '13 at 15:12
  • Okay, Ill let it stay open for a while for more opinions. Thank you – Nicki Sibbern Dec 17 '13 at 15:48

1 Answers1

1

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/>

Jens Mikkelsen
  • 2,712
  • 19
  • 23