如何向SyndicationItem添加一个SyndicationElementExtension

9
使用.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" />

大致意思是:

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

如何创建一个带有几个属性的简单SyndicationElementExtension?

2个回答

14

为了简化下一个试图弄清楚这个问题的人的理解,这里是一个工作示例,演示如何添加一个基本的项目缩略图(RSS 2.0 enclosure in this case),与文档类似:

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()
);

如果您想要一个简单的标记,您还可以转储属性并仅在标记名称后设置文本内容,例如<comments>http://my.comments/feed</comments>


你怎样转储属性?如果我没有传递任何内容,也没有为元素传递名称空间,它仍然会插入属性xmlns =“”。 - Hallaghan
嗯,我在我的端口没有看到这种情况发生,xmlns =""属性只在外部的<rss></rss>元素上指定。你能贴一个例子吗? - nickb

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());

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接