.Net SyndicationFeed serialization error when adding extensions to the RSS feed

409 views Asked by At

I create a .NET Framework SyndicationFeed:

SyndicationFeed feed = SyndicationFeed.Load(xmlReader);

Then I create a new item:

var newItem = new SyndicationItem();
newItem.Id = sourceFeedItem.ItemID;
newItem.Title = new TextSyndicationContent(sourceFeedItem.ItemTitle);

Then I add some iTunes item extensions:

var iTunesExt = newItem.ElementExtensions;
iTunesExt.Add(new XElement("MyElement", "Whatever");
newItem.ElementExtensions.Add(iTunesExt);

Then I add the item to the items list:

List<SyndicationItem> items = new List<SyndicationItem>();
items.Add(newItem);

and set the feed's Items property.

feed.Items = items;

and finally I write the SyndicationFeed feed to an XmlWriter.

feed.SaveAsRss20(xmlWriter);

All goes well if the extensions aren't added, in other words if the newItem.ElementExtensions.Add(iTunesExt); line doesn't execute. But if the line executes, I get the following error upon execution of feed.SaveAsRss20(xmlWriter);.

Type 'System.ServiceModel.Syndication.SyndicationElementExtension' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute.

How can I mark iTunesExt with the DataContractAttribute attribute? Or am I misunderstanding it?

1

There are 1 answers

1
Scott Hannen On
iTunesExt.Add(new XElement("MyElement", "Whatever");

Try modifying this to:

iTunesExt.Add(new XElement("MyElement", "Whatever"), 
    new DataContractSerializer(typeof(XElement)));

That allows you to specify use of the DataContractSerializer.