4

I need to add new namespaces to the rss(root) element of my feed, in addition to a10:

<rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
    <channel>
.
.
.

I am using a SyndicationFeed class serialized to RSS 2.0 and I use a XmlWriter to output the feed,

var feed = new SyndicationFeed(
                    feedDefinition.Title,
                    feedDefinition.Description,
     .
     .
     .



using (var writer = XmlWriter.Create(context.HttpContext.Response.Output, settings))
        {
            rssFormatter.WriteTo(writer);
        }

I have tried adding AttributeExtensions on SyndicationFeed but it adds the new namespaces to the channel element instead of the root,

Thank you

Amin
  • 73
  • 1
  • 7
  • Maybe you can write to a temp memorystream, in order to load the content in a XmlDocument where you can fix whatever you want. And only then, write the xml content to the output – Steve B Jan 18 '13 at 11:15

1 Answers1

4

Unfortunately the formatter is not extensible in the way you need.

You can use an intermediate XmlDocument and modify it before writing to the final output.

This code will add a namespace to the root element of the final xml output:

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());
jedigo
  • 901
  • 8
  • 12
  • Thank you, this is what I was looking for, although I did not use the StringBuilder, instead just used my XmlWriter.Create(context.HttpContext.Response.Output, settings). – Amin Jan 21 '13 at 10:25