除了a10之外,我还需要在我的feed的rss(root)元素中添加新的命名空间:
<rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
<channel>
.
.
.
我正在使用序列化为RSS 2.0的SyndicationFeed类,我使用XmlWriter输出feed,
var feed = new SyndicationFeed(
feedDefinition.Title,
feedDefinition.Description,
.
.
.
using (var writer = XmlWriter.Create(context.HttpContext.Response.Output, settings))
{
rssFormatter.WriteTo(writer);
}
我尝试在SyndicationFeed上添加AttributeExtensions,但它添加了新的命名空间
到通道元素而不是根,
谢谢
解决方法:
不幸的是,格式化程序不能以您需要的方式扩展.
您可以使用中间XmlDocument并在写入最终输出之前对其进行修改.
此代码将为最终xml输出的根元素添加命名空间:
var feed = new SyndicationFeed("foo", "bar", new Uri("http://www.example.com"));
var rssFeedFormatter = new Rss20FeedFormatter(feed);
// Create a new XmlDocument in order to modify the root element
var xmlDoc = new XmlDocument();
// Write the RSS formatted feed directly into the xml doc
using(var xw = xmlDoc.CreateNavigator().AppendChild() )
{
rssFeedFormatter.WriteTo(xw);
}
// modify the document as you want
xmlDoc.DocumentElement.SetAttribute("xmlns:example", "www.example.com");
// now create your writer and output to it:
var sb = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(sb))
{
xmlDoc.WriteTo(writer);
}
Console.WriteLine(sb.ToString());