次のようなスタイルシート参照を追加する必要があると思います
<?xml-stylesheet type="text/css" href="http://you.com/rss.css" ?>
しかし、.NET 4.5 SyndicationFeed または Atom10FeedFormatter クラスでこれを行う方法がわかりません。
これを行う方法の完全なコード例を次に示します。
using System;
using System.Collections.Generic;
using System.ServiceModel.Syndication;
using System.Xml;
namespace FeedProcessing
{
class FeedProcessing
{
static void Main(string[] args)
{
FeedProcessing fp = new FeedProcessing();
fp.CreateFeed();
}
public void CreateFeed()
{
SyndicationFeed feed = new SyndicationFeed("Feed Title", "Feed Description", new Uri("http://Feed/Alternate/Link"), "FeedID", DateTime.Now);
// Add a custom attribute.
XmlQualifiedName xqName = new XmlQualifiedName("CustomAttribute");
feed.AttributeExtensions.Add(xqName, "Value");
SyndicationPerson sp = new SyndicationPerson("jesper@contoso.com", "Jesper Aaberg", "http://Jesper/Aaberg");
feed.Authors.Add(sp);
SyndicationCategory category = new SyndicationCategory("FeedCategory", "CategoryScheme", "CategoryLabel");
feed.Categories.Add(category);
feed.Contributors.Add(new SyndicationPerson("lene@contoso.com", "Lene Aaling", "http://lene/aaling"));
feed.Copyright = new TextSyndicationContent("Copyright 2007");
feed.Description = new TextSyndicationContent("This is a sample feed");
// Add a custom element.
XmlDocument doc = new XmlDocument();
XmlElement feedElement = doc.CreateElement("CustomElement");
feedElement.InnerText = "Some text";
feed.ElementExtensions.Add(feedElement);
feed.Generator = "Sample Code";
feed.Id = "FeedID";
feed.ImageUrl = new Uri("http://server/image.jpg");
TextSyndicationContent textContent = new TextSyndicationContent("Some text content");
SyndicationItem item = new SyndicationItem("Item Title", textContent, new Uri("http://server/items"), "ItemID", DateTime.Now);
List<SyndicationItem> items = new List<SyndicationItem>();
items.Add(item);
feed.Items = items;
feed.Language = "en-us";
feed.LastUpdatedTime = DateTime.Now;
SyndicationLink link = new SyndicationLink(new Uri("http://server/link"), "alternate", "Link Title", "text/html", 1000);
feed.Links.Add(link);
XmlWriter atomWriter = XmlWriter.Create("atom.xml");
atomWriter.WriteProcessingInstruction("xml-stylesheet",
"type='text/css' href='http://you.com/rss.css'");
Atom10FeedFormatter atomFormatter = new Atom10FeedFormatter(feed);
atomFormatter.WriteTo(atomWriter);
atomWriter.Close();
XmlWriter rssWriter = XmlWriter.Create("rss.xml");
rssWriter.WriteProcessingInstruction("xml-stylesheet",
"type='text/css' href='http://you.com/rss.css'");
Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(feed);
rssFormatter.WriteTo(rssWriter);
rssWriter.Close();
}
}
}
このプログラムを実行すると、次の 2 つのファイルが作成されます。
アトム.xml :
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type='text/css' href='http://you.com/rss.css'?>
<feed xml:lang="en-us" CustomAttribute="Value" xmlns="http://www.w3.org/2005/Atom">
<title type="text">Feed Title</title>
<subtitle type="text">This is a sample feed</subtitle>
<id>FeedID</id>
<rights type="text">Copyright 2007</rights>
<updated>2012-12-28T19:50:18-08:00</updated>
<category term="FeedCategory" label="CategoryLabel" scheme="CategoryScheme" />
<logo>http://server/image.jpg</logo>
<author>
<name>Jesper Aaberg</name>
<uri>http://Jesper/Aaberg</uri>
<email>jesper@contoso.com</email>
</author>
<contributor>
<name>Lene Aaling</name>
<uri>http://lene/aaling</uri>
<email>lene@contoso.com</email>
</contributor>
<generator>Sample Code</generator>
<link rel="alternate" href="http://feed/Alternate/Link" />
<link rel="alternate" type="text/html" title="Link Title" length="1000" href="http://server/link" />
<CustomElement xmlns="">Some text</CustomElement>
<entry>
<id>ItemID</id>
<title type="text">Item Title</title>
<updated>2012-12-28T19:50:18-08:00</updated>
<link rel="alternate" href="http://server/items" />
<content type="text">Some text content</content>
</entry>
</feed>
およびrss.xml :
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type='text/css' href='http://you.com/rss.css'?>
<rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
<channel CustomAttribute="Value">
<title>Feed Title</title>
<link>http://feed/Alternate/Link</link>
<description>This is a sample feed</description>
<language>en-us</language>
<copyright>Copyright 2007</copyright>
<managingEditor>jesper@contoso.com</managingEditor>
<lastBuildDate>Fri, 28 Dec 2012 19:50:18 -0800</lastBuildDate>
<category domain="CategoryScheme">FeedCategory</category>
<generator>Sample Code</generator>
<a10:contributor>
<a10:name>Lene Aaling</a10:name>
<a10:uri>http://lene/aaling</a10:uri>
<a10:email>lene@contoso.com</a10:email>
</a10:contributor>
<image>
<url>http://server/image.jpg</url>
<title>Feed Title</title>
<link>http://feed/Alternate/Link</link>
</image>
<a10:id>FeedID</a10:id>
<a10:link rel="alternate" type="text/html" title="Link Title" length="1000" href="http://server/link" />
<CustomElement>Some text</CustomElement>
<item>
<guid isPermaLink="false">ItemID</guid>
<link>http://server/items</link>
<title>Item Title</title>
<description>Some text content</description>
<a10:updated>2012-12-28T19:50:18-08:00</a10:updated>
</item>
</channel>
</rss>
説明:
XmlWriter
インスタンスが作成されるときはいつでも、ただし次のいずれかを呼び出す前に:
atomFormatter.WriteTo(atomWriter);
また:
rssFormatter.WriteTo(rssWriter);
それぞれの XmlWriter インスタンスでメソッドを呼び出し、 2 つの必須パラメーター (PI の名前 (この場合は ) と PI のテキスト (この場合は ))WriteProcessingInstruction()
を渡します。"xml-stylesheet"
"type='text/css' href='http://you.com/rss.css'"