Soe*_*ren 1 c# rss syndication-feed
我正在使用一些自定义元素设置自定义 RSS 提要。我需要添加一个带有自定义属性的自定义元素。
到目前为止,我已经设置了这样的提要:
var testItem = new SyndicationItem("title", "description", new Uri("http://myuri.com"));
customItem.ElementExtensions.Add("customElement", String.Empty, "fooBar");
Run Code Online (Sandbox Code Playgroud)
将 testItem 添加到名为“items”的列表中,然后:
var feed = new SyndicationFeed("TestFeed", "FeedContent", new Uri("http://myuri.com"), items);
Run Code Online (Sandbox Code Playgroud)
这会产生这样的东西......
<rss>
<channel>
<title>TestFeed</title>
<link>http://myuri.com</link>
<description>FeedContent</description>
<item>
<link>http://myprovider.com/contentid=1234</link>
<title>title</title>
<description>description</description>
<customElement>fooBar</customElement>
</item>
</channel>
</rss>
Run Code Online (Sandbox Code Playgroud)
现在,如果我想添加自定义元素,然后向该元素添加自定义属性,该怎么办?
我可以像这样创建一个新的 SyndicateItem:
var customElement = new SyndicationItem();
Run Code Online (Sandbox Code Playgroud)
然后向其添加属性,如下所示:
customElement.AttributeExtensions.Add(new XmlQualifiedName("myAttribute", ""), "someValue");
customElement.AttributeExtensions.Add(new XmlQualifiedName("anotherAttribute"), "someOtherValue");
Run Code Online (Sandbox Code Playgroud)
然后将其添加到我的 testItem 中,使其出现在 rss feed 的项目列表中:
testItem.ElementExtensions.Add(customElement);
Run Code Online (Sandbox Code Playgroud)
编译器吃掉它,但我收到运行时错误,我认为这是因为新元素没有名称。
除此之外,我找不到其他方法来做到这一点
创建提要的 XmlDoc,然后开始向其附加元素和属性。
有必要这样做似乎很奇怪,而且我觉得我一定是监督了一些事情。
有任何想法吗?
找到了解决方案。
我可以像这样向 feed 添加一个项目:
var contentItem = new SyndicationItem("title", "description", new Uri("http://myuri.com"));
Run Code Online (Sandbox Code Playgroud)
然后添加自定义元素,如下所示:
contentItem.ElementExtensions.Add("customElement", String.Empty, "text inside my custom element");
Run Code Online (Sandbox Code Playgroud)
如果我想添加一个自定义元素并为其添加自定义属性;我可以:
contentItem.ElementExtensions.Add(new XElement("customImageElement", new XAttribute("type", "image/jpg"), new XAttribute("url", "www.myuri.com/pic1234.jpg")).CreateReader());
Run Code Online (Sandbox Code Playgroud)
这将输出:
<customImageElement type="image/jpg" url="www.myprovider.com/pic1234.jpg"></customImageElement>
Run Code Online (Sandbox Code Playgroud)
完成后,我将 contentItem 添加到 a List<SyndicationItem>,并在创建提要(项目)时添加此列表。
我还可以将自定义元素添加到 feed 本身的<channel>元素下:
首先添加包含项目列表的提要:
var feed = new SyndicationFeed("title text", "description text", new Uri("http://myuri.com"), items);
Run Code Online (Sandbox Code Playgroud)
然后将自定义元素添加到 feed 中。在元素下:
feed.ElementExtensions.Add(new XElement("image",
new XElement("url", null, "http://www.myuri.com/logo.jpg"),
new XElement("title", null, "MyImage"),
new XElement("link", null, "http://myuri.com/contentid=1234"),
new XElement("width", null, "100"),
new XElement("height", null, "100"),
new XElement("description", null, "This is my image")).CreateReader());
Run Code Online (Sandbox Code Playgroud)
这将输出:
<rss version="2.0">
<channel>
<title>title text</title>
<link>http://myuri.com</link>
<description>description text</description>
<image>
<url>http://www.myprovider.com/logo.jpg</url>
<title>MyImage</title>
<link>http://myprovider.com/contentid=1234</link>
<width>100</width>
<height>100</height>
<description>This is my image</description>
</image>
<item>
Items added to the items collection
...
...
...
</item>
</channel>
</rss>
Run Code Online (Sandbox Code Playgroud)
这就是我能想到的。如果有更好的方法,欢迎分享。
| 归档时间: |
|
| 查看次数: |
3546 次 |
| 最近记录: |