0

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?

Howiecamp
  • 2,981
  • 6
  • 38
  • 59

1 Answers1

0
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.

Scott Hannen
  • 27,588
  • 3
  • 45
  • 62
  • what I get now is `"An object of type 'System.Xml.Linq.XNodeReader' cannot be serialized at the top level for IXmlSerializable root type 'System.Xml.Linq.XElement' since its IsAny setting is 'true'. This type must write all its contents including the root element. Verify that the IXmlSerializable implementation is correct."`. I'm going to do some further resource because I'm not totally clear I even understand the errors yet. – Howiecamp Jul 17 '16 at 01:44