如何将SyndicationElementExtension添加到SyndicationItem

Sha*_*ler 8 .net c# syndication syndication-feed atom-feed

使用.NET System.ServiceModel.Syndication类...

我想向SyndicationItem添加一个新的SyndicationElementExtension,它将导出以下XML:

<media:thumbnail url="http://www.foo.com/keyframe.jpg" width="75" height="50" time="12:05:01.123" />
Run Code Online (Sandbox Code Playgroud)

有点像:

syndicationItem.ElementExtensions.Add(new SyndicationElementExtension("thumbnail", "http://video.search.yahoo.com/mrss", ?
Run Code Online (Sandbox Code Playgroud)

如何使用一些属性创建简单的SyndicationElementExtension?

nic*_*ckb 13

为了简化下一个试图解决这个问题的人,这里是一个在文档中添加基本项目缩略图(在这种情况下为RSS 2.0机箱)的工作示例:

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()
);
Run Code Online (Sandbox Code Playgroud)

如果你想要一个简单的标签,你也可以转储属性,只在标签名后面设置文本内容,即<comments>http://my.comments/feed</comments>.


Sha*_*ler 10

在这里找到答案:http://msdn.microsoft.com/en-us/library/bb943475.aspx

SyndicationElementExtensionCollection类还可用于从XmlReader实例创建元素扩展.这样可以轻松地与XML处理API(如XElement)集成,如以下示例代码所示.

feed.ElementExtensions.Add(new XElement("xElementExtension",
        new XElement("Key", new XAttribute("attr1", "someValue"), "Z"),
        new XElement("Value", new XAttribute("attr1", "someValue"), 
        "15")).CreateReader());
Run Code Online (Sandbox Code Playgroud)