Soeren Soeren - 2 years ago 138
C# Question

custom rss element with custom attribute C#

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(""));

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

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

var feed = new SyndicationFeed("TestFeed", "FeedContent", new Uri(""), items);

This would produce something like this...


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:


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?

Answer Source

Found a solution.

I can add an item to the feed like this:

var contentItem = new SyndicationItem("title", "description", new Uri(""));

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", "")).CreateReader());

This would output:

<customImageElement type="image/jpg" url=""></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(""), items);

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

feed.ElementExtensions.Add(new XElement("image",
            new XElement("url", null, ""),
            new XElement("title", null, "MyImage"),
            new XElement("link", null, ""),
            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">
    <title>title text</title>
    <description>description text</description>
      <description>This is my image</description>
      Items added to the items collection

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download