简体   繁体   English

如何将SyndicationElementExtension添加到SyndicationItem

[英]How To Add A SyndicationElementExtension To A SyndicationItem

Using the .NET System.ServiceModel.Syndication classes... 使用.NET System.ServiceModel.Syndication类...

I would like to add a new SyndicationElementExtension to a SyndicationItem that will export the following XML: 我想向SyndicationItem添加一个新的SyndicationElementExtension,它将导出以下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? 如何使用一些属性创建简单的SyndicationElementExtension?

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: 为了简化下一个试图解决这个问题的人,这里是一个在文档中添加基本项目缩略图(在这种情况下为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()
);

You can also dump the attributes and just set textual content after the tag name if you want a simple tag, ie <comments>http://my.comments/feed</comments> . 如果你想要一个简单的标签,你也可以转储属性,只在标签名后面设置文本内容,即<comments>http://my.comments/feed</comments>

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

The SyndicationElementExtensionCollection class can also be used to create element extensions from an XmlReader instance. SyndicationElementExtensionCollection类还可用于从XmlReader实例创建元素扩展。 This allows for easy integration with XML processing APIs such as XElement as shown in the following sample code. 这样可以轻松地与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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM