生成视频站点地图未按预期工作
Generating video sitemap not working quite as expected
好的,所以我在这里尝试了很多东西,但无法获得元素字符串的正确输出。
这是我想要的结果:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
这是我能得到的最接近的:
<urlset xmlns:video="http://www.google.com/schemas/sitemap-video/1.1" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" />
所以属性字符串从后到前(顺序错误)。
这是我正在使用的代码:
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");
此外,我尝试使用 this example 中的这段代码,但它给了我一个错误:
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");
这是我遇到的错误:
The prefix '' cannot be redefined from '' to 'http://www.sitemaps.org/schemas/sitemap/0.9' within the same start element tag.
我知道我在这里遗漏了一些东西只是不确定是什么,我也查看了 google 但找不到任何有用的东西。
我也试过改变函数变量的顺序,就是不能让它正常工作。
有人知道发生了什么事吗?
干杯
好的,明白了:
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");
将输出:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
下面是输出视频站点地图的完整代码,希望对大家有所帮助:
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();
更新
I modified the code as the price element needs the currency attribute. It's in between the /* Price */ comments
好的,所以我在这里尝试了很多东西,但无法获得元素字符串的正确输出。
这是我想要的结果:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
这是我能得到的最接近的:
<urlset xmlns:video="http://www.google.com/schemas/sitemap-video/1.1" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" />
所以属性字符串从后到前(顺序错误)。
这是我正在使用的代码:
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");
此外,我尝试使用 this example 中的这段代码,但它给了我一个错误:
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");
这是我遇到的错误:
The prefix '' cannot be redefined from '' to 'http://www.sitemaps.org/schemas/sitemap/0.9' within the same start element tag.
我知道我在这里遗漏了一些东西只是不确定是什么,我也查看了 google 但找不到任何有用的东西。 我也试过改变函数变量的顺序,就是不能让它正常工作。 有人知道发生了什么事吗? 干杯
好的,明白了:
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");
将输出:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
下面是输出视频站点地图的完整代码,希望对大家有所帮助:
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();
更新
I modified the code as the price element needs the currency attribute. It's in between the /* Price */ comments