9

Using the .NET System.ServiceModel.Syndication classes...

I would like to add a new SyndicationElementExtension to a SyndicationItem that will export the following XML:

<media:thumbnail url="http://www.foo.com/keyframe.jpg" width="75" height="50" time="12:05:01.123" />

Something along the lines of:

syndicationItem.ElementExtensions.Add(new SyndicationElementExtension("thumbnail", "http://video.search.yahoo.com/mrss", ?

How do you create a simple SyndicationElementExtension with a few attributes?

Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
Shawn Miller
  • 7,082
  • 6
  • 46
  • 54

2 Answers2

14

Just to simplify for the next guy who comes along trying to figure this out, here's a working example of adding a basic item thumbnail (RSS 2.0 enclosure in this case) along the lines of the documentation:

SyndicationItem item = new SyndicationItem();

// populate item...

item.ElementExtensions.Add(
    new XElement( "enclosure",
        new XAttribute( "type", "image/jpeg" ),
        new XAttribute( "url", "http://path.to/my/image.jpg" )
    ).CreateReader()
);

You can also dump the attributes and just set textual content after the tag name if you want a simple tag, i.e. <comments>http://my.comments/feed</comments>.

nickb
  • 2,870
  • 2
  • 25
  • 14
  • how do you dump the attributes? If I don't pass any and also don't pass the namespace for an element, it inserts the attribute xmlns="" anyways. – Hallaghan Jul 23 '12 at 11:19
  • Hmm, I'm not seeing that happening at my end, the `xmlns=""` attribute is only specified on the outer `` element. Can you post an example? – nickb Jul 28 '12 at 01:56
10

Found the answer here: http://msdn.microsoft.com/en-us/library/bb943475.aspx

The SyndicationElementExtensionCollection class can also be used to create element extensions from an XmlReader instance. This allows for easy integration with XML processing APIs such as XElement as shown in the following sample code.

feed.ElementExtensions.Add(new XElement("xElementExtension",
        new XElement("Key", new XAttribute("attr1", "someValue"), "Z"),
        new XElement("Value", new XAttribute("attr1", "someValue"), 
        "15")).CreateReader());
Shawn Miller
  • 7,082
  • 6
  • 46
  • 54