locked
System.ServiceModel.Syndication - Extensions RRS feed

  • Question

  •  

    Hi,

     

    Has anyone seen any examples or can anyone post code snippets for how to:

    1) Create custom extensions for syndicated feeds. The ExtensionElements.Add method has a number of overloads that make it look quite flexible but getting them all to work is causing some head-scratching.

    2) Consume feeds containing custom extensions such that the extensions are deserialized into their corresponding classes. This is something I have had absolutely no success wiht thus far.

     

    Thanks in advance,

    Phil

    Thursday, October 18, 2007 3:56 PM

Answers

  • You can add DataContract serializable classes, IXmlSerializable classes, XmlSerializer serializable classes and plain old XML as extensions to your feed, items, categories etc.

     

    On the consumption side, you can get a reader over all the extensions or consume by QName. You can also automatically deserialize the extensions.

     

    I'm including some sample snippets here - this is based on the RC0 Orcas bits but you can map a lot of this code to the beta2 equivalent code:

     

    Serializing:

     

    SyndicationFeed feed = new SyndicationFeed("Rss news feed", "this is a RSS feed", null, "feed-id", new DateTimeOffset(new DateTime(2007, 1, 1), TimeSpan.FromHours(12)));
     // show adding a data contract serializable type

     feed.ElementExtensions.Add(new RandomData(42));

     // show adding a data contract serializable type, customizing outer name and ns
     feed.ElementExtensions.Add("MyRandomData", "http://tmpuri.org", new RandomData(43));

    // show adding arbitrary XML

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<customer>joe</customer>");
    feed.ElementExtensions.Add(new XmlNodeReader(doc.DocumentElement));

     

     

    Consumption:

    feed = SyndicationFeed.Load(XmlReader.Create(stream));

    // enumerate all extensions, looking at outer name, ns

    foreach (SyndicationElementExtension extension in feed.ElementExtensions)
    {
             Console.WriteLine("extension: name={0}, namespace={1}", extension.OuterName, extension.OuterNamespace);
    }

    // deserialize the "random data" extension into a typed object

    RandomData rd = feed.ElementExtensions.ReadElementExtensions<RandomData>("RandomData", "http://schemas.datacontract.org/2004/07/FeedSerialization")[0];

    // get a reader over all the extensions

    XmlReader reader = feed.ElementExtensions.GetReaderAtElementExtensions()

     

     

        [DataContract]
        public class RandomData
        {
            int x;
            public RandomData(int x)
            {
                this.x = x;
            }
            [DataMember]
            public int X
            { get { return x; } set { x = value; } }
        }

     

     

    Hope this helps!


    Thanks

    Vish

     

     

    Thursday, October 18, 2007 9:25 PM