1

いくつかのカスタム要素を含むカスタム RSS フィードをセットアップしています。カスタム属性を含むカスタム要素を追加する必要があります。

これまでのところ、次のようなフィードを設定しました。

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

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

「items」という名前のリストに 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 を作成し、要素と属性の追加を開始します。

そうする必要があるのは奇妙に思えますし、何かを監督したに違いないと感じています..

何か案は?

4

1 に答える 1

4

解決策を見つけました。

次のようにフィードにアイテムを追加できます。

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>

それが私が思いつくことができたものです。より良い方法があれば、共有してください。

于 2013-05-29T06:56:59.253 に答える