2013-05-28 155 views
0

我设置了具有自定义元素的自定义rss源。我需要添加一个自定义元素在其中的自定义属性。具有自定义属性C的自定义rss元素#

到目前为止,我已经建立了一个饲料是这样的:

var testItem = new SyndicationItem("title", "description", new Uri("http://myuri.com")); 

customItem.ElementExtensions.Add("customElement", String.Empty, "fooBar"); 

添加testItem到一个名为“项目”列表中,然后:

var feed = new SyndicationFeed("TestFeed", "FeedContent", new Uri("http://myuri.com"), items); 

这将产生这样的事情..

<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> 

现在,如果我想同时添加一个自定义元素,然后向此元素添加自定义属性呢?

我可以创建一个新的SyndicationItem这样的:

var customElement = new SyndicationItem(); 

,然后添加属性,它是这样的:

customElement.AttributeExtensions.Add(new XmlQualifiedName("myAttribute", ""), "someValue"); 
customElement.AttributeExtensions.Add(new XmlQualifiedName("anotherAttribute"), "someOtherValue"); 

然后将其添加到我的testItem有它在我的名单在rss饲料中的项目:

testItem.ElementExtensions.Add(customElement); 

编译器吃了它,但我得到一个运行时错误,我认为这是因为新元素没有名字。

我找不到这样做的另一种方式,除了

创建饲料的xmlDoc中,然后开始追加要素和attibutes它。

它只是似乎不可思议,它应该有必要做到这一点,我觉得我必须有监督东西..

什么想法?

回答

2

找到了解决方案。

我可以将项目添加到饲料这样的:

var contentItem = new SyndicationItem("title", "description", new Uri("http://myuri.com")); 

,然后添加自定义元素这个是这样的:

contentItem.ElementExtensions.Add("customElement", String.Empty, "text inside my custom element"); 

如果我想添加自定义元素,并添加自定义属性;我可以这样做:

contentItem.ElementExtensions.Add(new XElement("customImageElement", new XAttribute("type", "image/jpg"), new XAttribute("url", "www.myuri.com/pic1234.jpg")).CreateReader()); 

这将输出:

<customImageElement type="image/jpg" url="www.myprovider.com/pic1234.jpg"></customImageElement> 

当我做,我添加contentItem到List<SyndicationItem>,当我创建提要(项目)增加这个列表。

我还可以添加自定义元素到饲料本身,<channel>元素:

首先添加饲料用的项目清单:

var feed = new SyndicationFeed("title text", "description text", new Uri("http://myuri.com"), items); 

然后添加自定义元素的饲料。该元素:

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

这将输出:

<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> 

这是我能想出。如果有更好的方法,请分享。

+0

我不能接受我自己的答案,直到明天,所以请不要殴打我:-) – Soeren

+0

我可以问你,什么样的JavaScript RSS阅读器会在那里读取你的rss版本2.0饲料,有自定义元素在里面?阅读本页 - http://www.w3schools.com/rss/rss_item.asp后,没有提到将自定义元素添加到区块或读取它们的方式。感谢您阅读您的自定义Feed的读者提供的任何建议。 – blackhawk

+1

我认为这个想法是你需要代码来阅读它们,但RSS feed的其余部分仍然可以在标准的RSS阅读器中查看。无论如何,这就是我们所做的。 –