Domanda

I am trying to make a video sitemap for video website. But I am facing a XMLExecption "The ':' character, hexadecimal value 0x3A, cannot be included in a name." This is because of colons (video:video) in the name.

XNamespace gs = "http://www.sitemaps.org/schemas/sitemap/0.9";
                XDocument doc = new XDocument(
                    new XElement(gs + "urlset",
                        (from p in db.Videos
                         orderby p.video_id descending
                         select new XElement(gs + "url",
                             new XElement(gs + "loc", "http://www.example.com/video/" + p.video_id + "-" + p.video_query),
                             new XElement(gs + "video:video",
                             new XElement(gs + "video:thumbnail_loc", "http://cdn.example.com/thumb/" + p.video_image)                           
                                 ))).Take(50)));
     doc.Save(@"C:\video_sitemap.xml");

Please tell me how to add colons in the name to generate dynamic xml sitemap using LINQ to SQL.

Thanks and Regards.

UPDATE:

This Video XML sitemap should look like on the page: Google Video Sitemap

È stato utile?

Soluzione

video here is the alias for a namespace. As per the example:

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"> 
   <url> 
     <loc>http://www.example.com/videos/some_video_landing_page.html</loc>
     <video:video>
       ...
     </video:video>
   </url>
</urlset>

So you just need two XNamespace values - one for the sitemap namespace and one for the video namespace:

XNamespace siteMapNs = "http://www.sitemaps.org/schemas/sitemap/0.9";
XNamespace videoNs = "http://www.google.com/schemas/sitemap-video/1.1";
XDocument doc = new XDocument(
    new XElement(siteMapNs + "urlset",
        (from p in db.Videos
         orderby p.video_id descending
         select new XElement(siteMapNs + "url",
             new XElement(siteMapNs + "loc",
                  "http://www.example.com/video/" + p.video_id + "-" + p.video_query),
             new XElement(videoNs + "video",
                 new XElement(videoNs + "thumbnail_loc",
                     "http://cdn.example.com/thumb/" + p.video_image)
             )
         )
        ).Take(50)
    )
);

EDIT: If you really want this to use an alias of video for the namespace, you can declare it in your root element:

XDocument doc = new XDocument(
    new XElement(siteMapNs + "urlset",
        new XAttribute(XNamespace.Xmlns + "video", videoNs),
        (from p in db.Videos
        ...

Altri suggerimenti

@Jon Skeet: Your code generated this sitemap

<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>http://www.example.com/video/1-video_query_1</loc>
    <video xmlns="http://www.google.com/schemas/sitemap-video/1.1">
      <thumbnail_loc>http://cdn.example.com/thumb/7665518872558731.jpg</thumbnail_loc>
    </video>
  </url>
  <url>
    <loc>http://www.example.com/video/2-video_query_2</loc>
    <video xmlns="http://www.google.com/schemas/sitemap-video/1.1">
      <thumbnail_loc>http://cdn.jigers.com/thumb/6921835997871337.jpg</thumbnail_loc>
    </video>
  </url>

but It should be like this:

    <?xml version="1.0" encoding="utf-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
      <url>
        <loc>http://www.example.com/video/1-video_query_1</loc>
        <video:video>
          <video:thumbnail_loc>http://cdn.example.com/thumb/7665518872558731.jpg</video:thumbnail_loc>
        </video:video>
      </url>
      <url>
        <loc>http://www.example.com/video/2-video_query_2</loc>
        <video:video>
          <video:thumbnail_loc>http://cdn.jigers.com/thumb/6921835997871337.jpg</video:thumbnail_loc>
        </video:video>
      </url>
</urlset>

video:video have colons and there should be a xmlns:video="http://www.google.com/schemas/sitemap-video/1.1 namespace in urlset..

Please take a look

You seem to be confusing aliases with namespaces.

Figure out what the namespace for 'videos'. Create an XNamespace for it (like you did in gs).

Then do videoNamespace + "thumbnail_loc".

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