M. Ali Iftikhar
M. Ali Iftikhar

Reputation: 3145

Using C# how to set mobile sitemap XML "urlset" (google recomendation)

I am trying to add google's recommended mobile sitemap header on to my page, which is:

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
  xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0">

Ref: http://support.google.com/webmasters/bin/answer.py?hl=en&answer=34648

If I use this (C#):

sitemap.WriteStartElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9");

it produces the following xml:

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

If I do it with prefix:

sitemap.WriteStartElement("mobile", "urlset", "http://www.google.com/schemas/sitemap-mobile/1.0");

Then I get the following:

<mobile:urlset mobile:xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

How can I achieve this?:

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
  xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0">

Upvotes: 1

Views: 746

Answers (2)

M. Ali Iftikhar
M. Ali Iftikhar

Reputation: 3145

Thanks @Candide ... I was able to resolve from your help, but had to tweak it a little. This is how i got it working:

sitemap.WriteStartElement("urlset");
sitemap.WriteAttributeString("xmlns", "http://www.google.com/schemas/sitemap-mobile/1.0");
sitemap.WriteAttributeString("xmlns:mobile", "http://www.google.com/schemas/sitemap-mobile/1.0");

this outputs on xml as:

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0">

Upvotes: 0

Roland Mai
Roland Mai

Reputation: 31077

Looking at the API the sitemap object appears to be XmlWriter. To write your custom namespace use the following:

sitemap.WriteStartElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9");
sitemap.WriteAttributeString("xmlns", "mobile", string.Empty, "http://www.google.com/schemas/sitemap-mobile/1.0");

Upvotes: 1

Related Questions