Search code examples
c#xml-sitemap

Generating video sitemap not working quite as expected


OK so I have tried numerous things here but can't get the output right for the element string.

Here is my desired result:

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

Here is the closest I can get:

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

So the attribute strings are back to front(is wrong order).

Here is the code that I'm using:

writer.WriteStartDocument();
writer.WriteStartElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9");
writer.WriteAttributeString("xmlns", "video", null, "http://www.google.com/schemas/sitemap-video/1.1");

Additionally I tried using this code from this example, but it gives me a error :

writer.WriteStartElement("urlset");
writer.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");
writer.WriteAttributeString("xmlns", "video", null, "http://www.google.com/schemas/sitemap-video/1.1");

This is the error I'm getting:

The prefix '' cannot be redefined from '' to 'http://www.sitemaps.org/schemas/sitemap/0.9' within the same start element tag.

I know I'm missing something here just not sure what, also I looked through google but can't find anything that helps. I have also tried changing the order of the function variables, just can't make it work right. Anyone know what's going on? Cheers


Solution

  • Ok figured it out:

    writer.WriteStartElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9");
    writer.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");
    writer.WriteAttributeString("xmlns", "video", null, "http://www.google.com/schemas/sitemap-video/1.1");
    

    Will output:

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

    Here is the full code to output video sitemap, hope it helps someone:

    writer.WriteStartDocument();
    writer.WriteStartElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9");
    writer.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");
    writer.WriteAttributeString("xmlns", "video", null, "http://www.google.com/schemas/sitemap-video/1.1");
    
    writer.WriteStartElement("url");
    writer.WriteElementString("loc", "https://youtube.com");
    writer.WriteStartElement("video", "video", "http://www.google.com/schemas/sitemap-video/1.1");
    writer.WriteElementString("video", "thumbnail_loc", null, "https://company.com/image-thumb.jpeg");
    writer.WriteElementString("video", "title", null, "this is the video title");
    writer.WriteElementString("video", "description", null, "this is a video description");
    writer.WriteElementString("video", "content_loc", null, "https://company.com/cool-product");
    writer.WriteElementString("video", "family_friendly", null, "yes");
    /* Price */
    writer.WriteStartElement("video", "price", "http://www.google.com/schemas/sitemap-video/1.1");
    writer.WriteAttributeString("currency", "AUD");
    writer.WriteString("100.00");
    writer.WriteEndElement();//video:uploader
    /* Price */
    writer.WriteElementString("video", "requires_subscription", null, "no");
    writer.WriteStartElement("video", "uploader", "http://www.google.com/schemas/sitemap-video/1.1");
    writer.WriteAttributeString("info", "https://company.com");
    writer.WriteString("My Company");
    writer.WriteEndElement();//video:uploader
    writer.WriteElementString("video", "live", null, "yes");
    writer.WriteEndElement();//video:video
    writer.WriteEndElement();//url
    
    writer.WriteEndElement();// urlset
    writer.WriteEndDocument();
    

    UPDATE

    I modified the code as the price element needs the currency attribute. It's in between the /* Price */ comments