Search code examples
c#rsssyndication-feed

Custom RSS element with custom attribute


I'm setting up a custom rss feed with som custom elements. I need to add a custom element with custom attributes in it.

So far I have set up a feed like this:

var testItem = new SyndicationItem("title", "description", new Uri("http://myuri.com"));

customItem.ElementExtensions.Add("customElement", String.Empty, "fooBar");

Add testItem to a list named "items", and then:

var feed = new SyndicationFeed("TestFeed", "FeedContent", new Uri("http://myuri.com"), items);

This would produce something like this...

<rss>
  <channel>
    <title>TestFeed</title>
    <link>http://myuri.com</link>
    <description>FeedContent</description>
    <item>
      <link>http://myprovider.com/contentid=1234</link>
      <title>title</title>
      <description>description</description>
      <customElement>fooBar</customElement>
    </item>
  </channel>
</rss>

Now, what if I want to both add a custom element, and then add custom attributes to this element?

I can create a new SyndicationItem like this:

var customElement = new SyndicationItem();

And then add attributes to it like this:

customElement.AttributeExtensions.Add(new XmlQualifiedName("myAttribute", ""), "someValue");
customElement.AttributeExtensions.Add(new XmlQualifiedName("anotherAttribute"), "someOtherValue");

And then add it to my testItem to have it in my list of items in the rss feed:

testItem.ElementExtensions.Add(customElement);

The compiler eats it, but I get a runtime error, and I think it's because the new element has no name.

I can't find another way of doing this, besides

creating a XmlDoc of the feed and then start appending elements and attibutes to it.

It just seems weird that it should be necessary to do that, and I feel I must have overseen something..

any ideas?


Solution

  • Found a solution.

    I can add an item to the feed like this:

    var contentItem = new SyndicationItem("title", "description", new Uri("http://myuri.com"));
    

    and then add custom elements to this like this:

    contentItem.ElementExtensions.Add("customElement", String.Empty, "text inside my custom element");
    

    If I want to add a custom element and add custom attributes to it; I can do:

    contentItem.ElementExtensions.Add(new XElement("customImageElement", new XAttribute("type", "image/jpg"), new XAttribute("url", "www.myuri.com/pic1234.jpg")).CreateReader());
    

    This would output:

    <customImageElement type="image/jpg" url="www.myprovider.com/pic1234.jpg"></customImageElement>
    

    When I'm done, I add the contentItem to a List<SyndicationItem>, and add this list when I create the feed (items).

    I could also add custom elements to the feed itself, under the <channel> element:

    First add the feed with a list of items:

    var feed = new SyndicationFeed("title text", "description text", new Uri("http://myuri.com"), items);
    

    Then add the custom elements to the feed. under the element:

    feed.ElementExtensions.Add(new XElement("image",
                new XElement("url", null, "http://www.myuri.com/logo.jpg"),
                new XElement("title", null, "MyImage"),
                new XElement("link", null, "http://myuri.com/contentid=1234"),
                new XElement("width", null, "100"),
                new XElement("height", null, "100"),
                new XElement("description", null, "This is my image")).CreateReader());
    

    This would output:

    <rss version="2.0">
    <channel>
        <title>title text</title>
        <link>http://myuri.com</link>
        <description>description text</description>
        <image>
          <url>http://www.myprovider.com/logo.jpg</url>
          <title>MyImage</title>
          <link>http://myprovider.com/contentid=1234</link>
          <width>100</width>
          <height>100</height>
          <description>This is my image</description>
        </image>
        <item>
          Items added to the items collection
          ...
          ...
          ...
        </item>
      </channel>
    </rss>
    

    That's what I could come up with. If there is a better way, please share.