如何使用 mvcsitemapprovider 添加自定义 xml 标签到 sitemap.xml?
How to add custom xml tags to sitemap.xml using mvcsitemapprovider?
根据 Google 在 Video sitemaps 中定义的内容,有一些 xml 标签应该添加到视频的站点地图中,并且应该是这样的:
<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:thumbnail_loc>http://www.example.com/thumbs/123.jpg</video:thumbnail_loc>
<video:title>Grilling steaks for summer</video:title>
<video:description>Alkis shows you how to get perfectly done steaks every
time</video:description>
<video:content_loc>http://www.example.com/video123.mp4</video:content_loc>
<video:player_loc autoplay="ap=1">
http://www.example.com/videoplayer.mp4?video=123</video:player_loc>
<video:duration>600</video:duration>
<video:expiration_date>2009-11-05T19:20:30+08:00</video:expiration_date>
<video:rating>4.2</video:rating>
<video:view_count>12345</video:view_count>
<video:publication_date>2007-11-05T19:20:30+08:00</video:publication_date>
<video:family_friendly>yes</video:family_friendly>
<video:restriction relationship="allow">IE GB US CA</video:restriction>
<video:gallery_loc title="Cooking Videos">http://cooking.example.com</video:gallery_loc>
<video:price currency="EUR">1.99</video:price>
<video:requires_subscription>yes</video:requires_subscription>
<video:uploader info="http://www.example.com/users/grillymcgrillerson">GrillyMcGrillerson
</video:uploader>
<video:live>no</video:live>
</video:video>
我想知道如何将这些自定义标签动态添加到我的 sitemap.xml?我正在使用 mvcsitemapprovider
由于 XML 站点地图功能不是 MvcSiteMapProvider 的主要目的,而是一个 "extra" 功能,因此它不是很灵活。
我开始致力于创建一种通用的方式来构建(和页面)XML 站点地图并支持 Google 的完整规范(包括视频)。但意识到这应该是一个完全独立于 MvcSiteMapProvider 的组件,并且从未发布过它。欢迎您从here.
中获取您需要的东西
好吧,我还不能使用 mvcsitemapprovider 来实现我的目标,但我做了这个临时解决方案,我正在尝试从中制作 nuget 包或将其作为功能添加到 mvcsitemapprovider 包中,这是代码我添加到控制器中的 ContentResult 并更改了 routeConfig 以在 url 看起来像这样时调用此方法 "/videoSiteMap.xml" :
public ContentResult VideoSiteMap()
{
XmlDocument xmlDoc = new XmlDocument();
using (XmlWriter writer = xmlDoc.CreateNavigator().AppendChild())
{
//writer.Formatting = Formatting.Indented;
writer.WriteStartDocument();
writer.WriteStartElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9");
// add namespaces
writer.WriteAttributeString("xmlns", "video", null, "http://www.google.com/schemas/sitemap-video/1.1");
List<VideoSiteMap> siteMapp = null;
siteMapp = ServiceHelper.GetGoogleSiteMap();//I invoked a service
//you can use a fake loop instead: for (int i = 0; i < 10; i++)
foreach( var content in siteMapp)
{
writer.WriteStartElement("url");
// required
writer.WriteElementString("loc", "http://example.com/myplayer.aspx");
writer.WriteStartElement("video", "video", null);
// start:optional
writer.WriteElementString("video", "thumbnail_loc", null, "http://www.example.com/thumbs/123.jpg");
writer.WriteElementString("video", "title", null, "");
writer.WriteElementString("video", "description", null, "Alkis shows you how to get perfectly done steaks every time");
writer.WriteElementString("video", "content_loc", null, "http://www.example.com/video123.mp4");
writer.WriteStartElement("video", "player_loc", null);
writer.WriteAttributeString("autoplay", "ap=1");
writer.WriteString("http://www.example.com/videoplayer.mp4?video=123");
writer.WriteEndElement(); // video:player_loc
// end:optional
writer.WriteElementString("video", "duration", null, "100");
writer.WriteElementString("video", "expiration_date", null, "2009-11-05T19:20:30+08:00");
writer.WriteElementString("video", "rating", null, "4.2");
writer.WriteElementString("video", "view_count", null, "12345");
writer.WriteElementString("video", "publication_date", null, "2007-11-05T19:20:30+08:00");
writer.WriteElementString("video", "family_friendly", null, "yes");
writer.WriteElementString("video", "category", null, "Cooking");
writer.WriteStartElement("video", "restriction", null);
writer.WriteAttributeString("relationship", "allow");
writer.WriteString("IE GB US CA");
writer.WriteEndElement();
writer.WriteStartElement("video", "gallery_loc", null);
writer.WriteAttributeString("title", "Cooking Videos");
writer.WriteString("http://cooking.example.com");
writer.WriteEndElement();
writer.WriteStartElement("video", "price", null);
writer.WriteAttributeString("currency", "EUR");
writer.WriteString("1.99");
writer.WriteEndElement();
writer.WriteElementString("video", "requires_subscription", null, "yes");
writer.WriteStartElement("video", "uploader", null);
writer.WriteAttributeString("info", "http://www.example.com/users/grillymcgrillerson");
writer.WriteString("GrillyMcGrillerson");
writer.WriteEndElement();
writer.WriteElementString("video", "live", null, "No");
writer.WriteEndElement(); // video:video
writer.WriteEndElement(); //url
}
writer.WriteEndElement(); //urlset
writer.WriteEndDocument();
writer.Close();
}
var stringWriter = new StringWriter();
var xmlTextWriter = XmlWriter.Create(stringWriter);
xmlDoc.WriteTo(xmlTextWriter);
xmlTextWriter.Flush();
return Content(stringWriter.GetStringBuilder().ToString().replace("utf-16","utf-8"), "text/xml", Encoding.UTF8);
}
我在返回 xml 数据时用 utf-8 替换了 utf-16,因为我找不到更简单的方法来更改 XmlWriter 的结果(默认情况下它总是 returns utf-16 格式)
我希望它能帮助其他人,如果有人帮我把它做成一个 nuget 包之类的,我会很高兴:D
根据 Google 在 Video sitemaps 中定义的内容,有一些 xml 标签应该添加到视频的站点地图中,并且应该是这样的:
<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:thumbnail_loc>http://www.example.com/thumbs/123.jpg</video:thumbnail_loc>
<video:title>Grilling steaks for summer</video:title>
<video:description>Alkis shows you how to get perfectly done steaks every
time</video:description>
<video:content_loc>http://www.example.com/video123.mp4</video:content_loc>
<video:player_loc autoplay="ap=1">
http://www.example.com/videoplayer.mp4?video=123</video:player_loc>
<video:duration>600</video:duration>
<video:expiration_date>2009-11-05T19:20:30+08:00</video:expiration_date>
<video:rating>4.2</video:rating>
<video:view_count>12345</video:view_count>
<video:publication_date>2007-11-05T19:20:30+08:00</video:publication_date>
<video:family_friendly>yes</video:family_friendly>
<video:restriction relationship="allow">IE GB US CA</video:restriction>
<video:gallery_loc title="Cooking Videos">http://cooking.example.com</video:gallery_loc>
<video:price currency="EUR">1.99</video:price>
<video:requires_subscription>yes</video:requires_subscription>
<video:uploader info="http://www.example.com/users/grillymcgrillerson">GrillyMcGrillerson
</video:uploader>
<video:live>no</video:live>
</video:video>
我想知道如何将这些自定义标签动态添加到我的 sitemap.xml?我正在使用 mvcsitemapprovider
由于 XML 站点地图功能不是 MvcSiteMapProvider 的主要目的,而是一个 "extra" 功能,因此它不是很灵活。
我开始致力于创建一种通用的方式来构建(和页面)XML 站点地图并支持 Google 的完整规范(包括视频)。但意识到这应该是一个完全独立于 MvcSiteMapProvider 的组件,并且从未发布过它。欢迎您从here.
中获取您需要的东西好吧,我还不能使用 mvcsitemapprovider 来实现我的目标,但我做了这个临时解决方案,我正在尝试从中制作 nuget 包或将其作为功能添加到 mvcsitemapprovider 包中,这是代码我添加到控制器中的 ContentResult 并更改了 routeConfig 以在 url 看起来像这样时调用此方法 "/videoSiteMap.xml" :
public ContentResult VideoSiteMap()
{
XmlDocument xmlDoc = new XmlDocument();
using (XmlWriter writer = xmlDoc.CreateNavigator().AppendChild())
{
//writer.Formatting = Formatting.Indented;
writer.WriteStartDocument();
writer.WriteStartElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9");
// add namespaces
writer.WriteAttributeString("xmlns", "video", null, "http://www.google.com/schemas/sitemap-video/1.1");
List<VideoSiteMap> siteMapp = null;
siteMapp = ServiceHelper.GetGoogleSiteMap();//I invoked a service
//you can use a fake loop instead: for (int i = 0; i < 10; i++)
foreach( var content in siteMapp)
{
writer.WriteStartElement("url");
// required
writer.WriteElementString("loc", "http://example.com/myplayer.aspx");
writer.WriteStartElement("video", "video", null);
// start:optional
writer.WriteElementString("video", "thumbnail_loc", null, "http://www.example.com/thumbs/123.jpg");
writer.WriteElementString("video", "title", null, "");
writer.WriteElementString("video", "description", null, "Alkis shows you how to get perfectly done steaks every time");
writer.WriteElementString("video", "content_loc", null, "http://www.example.com/video123.mp4");
writer.WriteStartElement("video", "player_loc", null);
writer.WriteAttributeString("autoplay", "ap=1");
writer.WriteString("http://www.example.com/videoplayer.mp4?video=123");
writer.WriteEndElement(); // video:player_loc
// end:optional
writer.WriteElementString("video", "duration", null, "100");
writer.WriteElementString("video", "expiration_date", null, "2009-11-05T19:20:30+08:00");
writer.WriteElementString("video", "rating", null, "4.2");
writer.WriteElementString("video", "view_count", null, "12345");
writer.WriteElementString("video", "publication_date", null, "2007-11-05T19:20:30+08:00");
writer.WriteElementString("video", "family_friendly", null, "yes");
writer.WriteElementString("video", "category", null, "Cooking");
writer.WriteStartElement("video", "restriction", null);
writer.WriteAttributeString("relationship", "allow");
writer.WriteString("IE GB US CA");
writer.WriteEndElement();
writer.WriteStartElement("video", "gallery_loc", null);
writer.WriteAttributeString("title", "Cooking Videos");
writer.WriteString("http://cooking.example.com");
writer.WriteEndElement();
writer.WriteStartElement("video", "price", null);
writer.WriteAttributeString("currency", "EUR");
writer.WriteString("1.99");
writer.WriteEndElement();
writer.WriteElementString("video", "requires_subscription", null, "yes");
writer.WriteStartElement("video", "uploader", null);
writer.WriteAttributeString("info", "http://www.example.com/users/grillymcgrillerson");
writer.WriteString("GrillyMcGrillerson");
writer.WriteEndElement();
writer.WriteElementString("video", "live", null, "No");
writer.WriteEndElement(); // video:video
writer.WriteEndElement(); //url
}
writer.WriteEndElement(); //urlset
writer.WriteEndDocument();
writer.Close();
}
var stringWriter = new StringWriter();
var xmlTextWriter = XmlWriter.Create(stringWriter);
xmlDoc.WriteTo(xmlTextWriter);
xmlTextWriter.Flush();
return Content(stringWriter.GetStringBuilder().ToString().replace("utf-16","utf-8"), "text/xml", Encoding.UTF8);
}
我在返回 xml 数据时用 utf-8 替换了 utf-16,因为我找不到更简单的方法来更改 XmlWriter 的结果(默认情况下它总是 returns utf-16 格式)
我希望它能帮助其他人,如果有人帮我把它做成一个 nuget 包之类的,我会很高兴:D