Custom tags and attributes in a SyndicationItem
for those of you who are not quite satisfied with the standard syndication functionality of .Net, here is a small tip on how to extend the XML generated from the SyndicationFeed class in C#.
Let’s assume we would like to have a RSS feed, which in fact would serve as a podcast, e.g. for iTunes. The software from Apple uses some information from their custom-defined RSS-tags, with an “itunes” prefix, for example:
Without this prefix it’s very easy. Our SyndicationItem class provides us a functionality to extend the standard item’s elements:
SyndicationItem item = new SyndicationItem(); item.ElementExtensions.Add("customTag", String.Empty, "My test");
The second attribute is the namespace which comes into play in the next step. In order to add the tag prefix as mentioned before, one has start with adding the namespace to the feed instance:
SyndicationFeed feed = new SyndicationFeed(); XmlQualifiedName n=new XmlQualifiedName("itunes","http://www.w3.org/2000/xmlns/"); String itunesNs = "http://www.itunes.com/dtds/podcast-1.0.dtd"; feed.AttributeExtensions.Add(n, itunesNs);
Now that we have added the new namespace to the feed, we can start adding custom item elements within that namespace.
SyndicationItem item = new SyndicationItem(); item.ElementExtensions.Add(new SyndicationElementExtension("author", itunesNs, "Famous author"));
That should solve the issue with custom tags with custom prefixes. One more thing.. it may be quite useful for those which attach media files to their podcasts: the enclosure tag.
In order to add such an element to the SyndicationItem, we can use the SyndicationLink object as follows:
SyndicationLink enclosure = SyndicationLink.CreateMediaEnclosureLink( new Uri("http://example.com/01.mp3"), "audio/mpeg", 1200); item.Links.Add(enclosure);
The method takes three arguments: the URL , type (MIME-type) and length of media attachment.
Hope this helps.
~ by Łukasz on 10 June 2010.