WCF 新聞訂閱物件模型對應到 Atom 和 RSS 的方式
在您開發 Windows Communication Foundation (WCF) 新聞訂閱服務時,會使用下列類別來建立摘要與項目:
- SyndicationFeed
- SyndicationItem
- SyndicationPerson
- SyndicationLink
- SyndicationCategory
- TextSyndicationContent
- UrlSyndicationContent
- XmlSyndicationContent
SyndicationFeed 可以序列化為任何一種用來定義格式器的新聞訂閱格式。WCF 會隨附兩個格式器:Atom10FeedFormatter 和 Rss20FeedFormatter。
在 SyndicationFeed 和 SyndicationItem 周圍的物件模型會與 Atom 1.0 規格 (而不是 RSS 2.0 規格) 較為符合。這是因為 Atom 1.0 是比較實質上的規格,可用來定義不明確或是從 RSS 2.0 規格中省略的項目。由於這個原因,WCF 新聞訂閱物件模型中的許多項目並未在 RSS 2.0 規格中具有直接代表項。當您將 SyndicationFeed 和 SyndicationItem 物件序列化為 RSS 2.0 時,WCF 可讓您將 Atom 特定的資料項目序列化為符合命名空間延伸項目以符合 Atom 規格。您可以將參數傳遞至 Rss20FeedFormatter 建構函式來控制這個項目。
本主題中的程式碼範例將使用此處所定義的兩種方法之一來實際執行序列化作業。
SerializeFeed
會序列化新聞訂閱摘要。
SerializeItem
會序列化新聞訂閱項目。
SyndicationFeed
下列程式碼範例說明將 SyndicationFeed 類別序列化為 Atom 1.0 與 RSS 2.0 的方式。
下列 XML 說明 SyndicationFeed 將如何序列化為 Atom 1.0。
<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="EN-US" xmlns="http://www.w3.org/2005/Atom">
<title type="text">My Feed Title</title>
<subtitle type="text">My Feed Description</subtitle>
<id>FeedID</id>
<rights type="text">Copyright 2007</rights>
<updated>2007-08-29T13:57:17-07:00</updated>
<category term="categoryName" label="categoryLabel" scheme="categoryScheme" />
<logo>https://server/image.jpg</logo>
<generator>Sample Code</generator>
<link rel="alternate" href="http://myfeeduri/" />
<entry>
<id>ItemID</id>
<title type="text">Item Title</title>
<summary type="text">Item Summary</summary>
<published>2007-08-29T00:00:00-07:00</published>
<updated>2007-08-29T13:57:17-07:00</updated>
<author>
<name>Jesper Aaberg</name>
<uri>http://Jesper/Aaberg</uri>
<email>Jesper@Aaberg.com</email>
</author>
<contributor>
<name>Lene Aaling</name>
<uri>http://Lene/Aaling</uri>
<email>Lene@Aaling.com</email>
</contributor>
<link rel="alternate" href="http://myitemuri/" />
<category term="categoryName" label="categoryLabel" scheme="categoryScheme" />
<content type="text">Item Content</content>
<rights type="text">Copyright 2007</rights>
<source>
<title type="text">My Feed Title</title>
<subtitle type="text">My Feed Description</subtitle>
<id>FeedID</id>
<rights type="text">Copyright 2007</rights>
<updated>2007-08-29T13:57:17-07:00</updated>
<category term="categoryName" label="categoryLabel" scheme="categoryScheme" />
<logo>https://server/image.jpg</logo>
<generator>Sample Code</generator>
<link rel="alternate" href="http://myfeeduri/" />
</source>
</entry>
</feed>
下列 XML 說明 SyndicationFeed 將如何序列化為 RSS 2.0。
<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>My Feed Title</title>
<link>http://myfeeduri/</link>
<description>My Feed Description</description>
<language>EN-US</language>
<copyright>Copyright 2007</copyright>
<lastBuildDate>Wed, 29 Aug 2007 13:57:17 -0700</lastBuildDate>
<category domain="categoryScheme">categoryName</category>
<generator>Sample Code</generator>
<image>
<url>https://server/image.jpg</url>
<title>My Feed Title</title>
<link>http://myfeeduri/</link>
</image>
<a10:id>FeedID</a10:id>
<item>
<guid isPermaLink="false">ItemID</guid>
<link>http://myitemuri/</link>
<author>Jesper@Aaberg.com</author>
<category domain="categoryScheme">categoryName</category>
<title>Item Title</title>
<description>Item Summary</description>
<source>My Feed Title</source>
<pubDate>Wed, 29 Aug 2007 00:00:00 -0700</pubDate>
<a10:updated>2007-08-29T13:57:17-07:00</a10:updated>
<a10:rights type="text">Copyright 2007</a10:rights>
<a10:content type="text">Item Content</a10:content>
<a10:contributor>
<a10:name>Lene Aaling</a10:name>
<a10:uri>http://Lene/Aaling</a10:uri>
<a10:email>Lene@Aaling.com</a10:email>
</a10:contributor>
</item>
</channel>
</rss>
SyndicationItem
下列程式碼範例說明將 SyndicationItem 類別序列化為 Atom 1.0 與 RSS 2.0 的方式。
下列 XML 說明 SyndicationItem 將如何序列化為 Atom 1.0。
<entry xmlns="http://www.w3.org/2005/Atom">
<id>ItemID</id>
<title type="text">Item Title</title>
<summary type="text">Item Summary</summary>
<published>2007-08-29T00:00:00-07:00</published>
<updated>2007-08-29T14:07:09-07:00</updated>
<author>
<name>Jesper Aaberg</name>
<uri>http://Contoso/Aaberg</uri>
<email>Jesper.Aaberg@contoso.com</email>
</author>
<author>
<name>Syed Abbas</name>
<uri>http://Contoso/Abbas</uri>
<email>Syed.Abbas@contoso.com</email>
</author>
<contributor>
<name>Lene Aaling</name>
<uri>http://Contoso/Aaling</uri>
<email>Lene.Aaling@contoso.com</email>
</contributor>
<contributor>
<name>Kim Abercrombie</name>
<uri>http://Contoso/Abercrombie</uri>
<email>Kim.Abercrombie@contoso.com</email>
</contributor>
<link rel="alternate" href="http://myitemuri/" />
<category term="categoryName" label="categoryLabel" scheme="categoryScheme" />
<category term="categoryName" label="categoryLabel" scheme="categoryScheme" />
<content type="text">Item Content</content>
<rights type="text">Copyright 2007</rights>
<source>
<title type="text">My Feed Title</title>
<subtitle type="text">My Feed Description</subtitle>
<link rel="alternate" href="http://myfeeduri/" />
</source>
</entry>
下列 XML 說明 SyndicationItem 將如何序列化為 RSS 2.0。
<item>
<guid isPermaLink="false">ItemID</guid>
<link>http://myitemuri/</link>
<author xmlns="http://www.w3.org/2005/Atom">
<name>Jesper Aaberg</name>
<uri>http://Jesper/Aaberg</uri>
<email>Jesper@Aaberg.com</email>
</author>
<author xmlns="http://www.w3.org/2005/Atom">
<name>Syed Abbas</name>
<uri>http://Contoso/Abbas</uri>
<email>Syed.Abbas@contoso.com</email>
</author>
<category domain="categoryScheme">categoryName</category>
<category domain="categoryScheme">categoryName</category>
<title>Item Title</title>
<description>Item Summary</description>
<source>My Feed Title</source>
<pubDate>Wed, 29 Aug 2007 00:00:00 -0700</pubDate>
<updated xmlns="http://www.w3.org/2005/Atom">2007-08-29T14:07:09-07:00</updated>
<rights type="text" xmlns="http://www.w3.org/2005/Atom">Copyright 2007</rights>
<content type="text" xmlns="http://www.w3.org/2005/Atom">Item Content</content>
<contributor xmlns="http://www.w3.org/2005/Atom">
<name>Lene Aaling</name>
<uri>http://Contoso/Aaling</uri>
<email>Lene.Aaling@contoso.com</email>
</contributor>
<contributor xmlns="http://www.w3.org/2005/Atom">
<name>Kim Abercrombie</name>
<uri>http://Contoso/Abercrombie</uri>
<email>Kim.Abercrombie@contoso.com</email>
</contributor>
</item>
SyndicationPerson
下列程式碼範例說明將 SyndicationPerson 類別序列化為 Atom 1.0 與 RSS 2.0 的方式。
下列 XML 說明 SyndicationPerson 將如何序列化為 Atom 1.0。
<author>
<name>Jesper Aaberg</name>
<uri>http://Contoso/Aaberg</uri>
<email>Jesper.Aaberg@contoso.com</email>
</author>
<contributor>
<name>Lene Aaling</name>
<uri>http://Contoso/Aaling</uri>
<email>Lene.Aaling@contoso.com</email>
</contributor>
下列 XML 說明如果只有一個 SyndicationPerson 分別存在於 Authors 或 Contributors 集合的話,SyndicationPerson 類別將如何序列化為 RSS 2.0。
<author>Jesper.Aaberg@contoso.com</author>
<a10:contributor>
<a10:name>Lene Aaling</a10:name>
<a10:uri>http://Contoso/Aaling</a10:uri>
<a10:email>Lene.Aaling@contoso.com</a10:email>
</a10:contributor>
下列 XML 說明如果有一個以上的 SyndicationPerson 分別存在於 Authors 或 Contributors 集合的話,SyndicationPerson 類別將如何序列化為 RSS 2.0。
<a10:author>
<a10:name>Jesper Aaberg</a10:name>
<a10:uri>http://Contoso/Aaberg</a10:uri>
<a10:email>Jesper.Aaberg@contoso.com</a10:email>
</a10:author>
<a10:author>
<a10:name>Syed Abbas</a10:name>
<a10:uri>http://Contoso/Abbas</a10:uri>
<a10:email>Syed.Abbas@contoso.com</a10:email>
</a10:author>
<a10:contributor>
<a10:name>Lene Aaling</a10:name>
<a10:uri>http://Contoso/Aaling</a10:uri>
<a10:email>Lene.Aaling@contoso.com</a10:email>
</a10:contributor>
<a10:contributor>
<a10:name>Kim Abercrombie</a10:name>
<a10:uri>http://Contoso/Abercrombie</a10:uri>
<a10:email>Kim.Abercrombie@contoso.com</a10:email>
</a10:contributor>
SyndicationLink
下列程式碼範例說明將 SyndicationLink 類別序列化為 Atom 1.0 與 RSS 2.0 的方式。
下列 XML 說明 SyndicationLink 將如何序列化為 Atom 1.0。
<link rel="alternate" type="text/html" title="My Link Title" length="2048" href="http://contoso/MyLink" />
下列 XML 說明 SyndicationLink 將如何序列化為 RSS 2.0。
<a10:link rel="alternate" type="text/html" title="My Link Title" length="2048" href="http://contoso/MyLink" />
SyndicationCategory
下列程式碼範例說明將 SyndicationCategory 類別序列化為 Atom 1.0 與 RSS 2.0 的方式。
下列 XML 說明 SyndicationCategory 將如何序列化為 Atom 1.0。
<category term="categoryName" label="categoryLabel" scheme="categoryScheme" />
下列 XML 說明 SyndicationCategory 將如何序列化為 RSS 2.0。
<category domain="categoryScheme">categoryName</category>
TextSyndicationContent
下列程式碼範例說明當您使用 HTML 內容來建立 TextSyndicationContent 的時候,將 TextSyndicationContent 類別序列化為 Atom 1.0 與 RSS 2.0 的方式。
下列 XML 說明使用 HTML 內容的 TextSyndicationContent 類別將如何序列化為 Atom 1.0 的方式。
<content type="html"><html> some html </html></content>
下列 XML 說明使用 HTML 內容的 TextSyndicationContent 類別將如何序列化為 RSS 2.0 的方式。
<description><html> some html </html></description>
下列程式碼範例說明當您使用純文字內容來建立 TextSyndicationContent 的時候,將 TextSyndicationContent 類別序列化為 Atom 1.0 與 RSS 2.0 的方式。
下列 XML 說明使用純文字內容的 TextSyndicationContent 類別將如何序列化為 Atom 1.0 的方式。
<content type="text">Some Plain Text</content>
下列 XML 說明使用純文字內容的 TextSyndicationContent 類別將如何序列化為 RSS 2.0 的方式。
<description>Some Plain Text</description>
下列程式碼範例說明當您使用 XHTML 內容來建立 TextSyndicationContent 的時候,將 TextSyndicationContent 類別序列化為 Atom 1.0 與 RSS 2.0 的方式。
下列 XML 說明使用 XHTML 內容的 TextSyndicationContent 類別將如何序列化為 Atom 1.0 的方式。
<content type="xhtml">
<html> some xhtml </html>
</content>
下列 XML 說明使用 XHTML 內容的 TextSyndicationContent 類別將如何序列化為 RSS 2.0 的方式。
<description><html> some xhtml </html></description>
UrlSyndicationContent
下列程式碼範例說明將 UrlSyndicationContent 類別序列化為 Atom 1.0 與 RSS 2.0 的方式。
下列 XML 說明 UrlSyndicationContent 類別如何序列化為 Atom 1.0 的方式。
<content type="audio" src="http://someurl/" />
下列 XML 說明使用 XHTML 內容的 UrlSyndicationContent 類別將如何序列化為 RSS 2.0 的方式。
<description />
<content type="audio" src="http://Contoso/someurl/" xmlns="http://www.w3.org/2005/Atom" />
XmlSyndicationContent
下列程式碼範例說明將 XmlSyndicationContent 類別序列化為 Atom 1.0 與 RSS 2.0 的方式。
下列 XML 說明 XmlSyndicationContent 類別如何序列化為 Atom 1.0 的方式。
<content type="mytype">
<SomeData xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/FeedMapping" />
</content>
下列 XML 說明使用 XHTML 內容的 XmlSyndicationContent 類別將如何序列化為 RSS 2.0 的方式。
<content type="mytype" xmlns="http://www.w3.org/2005/Atom">
<SomeData xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/FeedMapping" />
</content>
請參閱
工作
HOW TO:建立基本 RSS 摘要
HOW TO:建立基本 Atom 摘要
HOW TO:將摘要同時公開為 Atom 和 RSS