2011-08-02 36 views
1

我使用.NET SyndicationFeed类来创建消耗的RSS提要,但我需要将XSLT样式表链接到生成的XML,以便为Web浏览器设置样式。我一直无法找到一个简单的方法来做到这一点。XSLT to XHTML for C#.NET SyndicationFeed

在Feed生成之后,我最好是自己将<?xml-stylesheet ... ?>标记插入生成的XML中,还是有更优雅的解决方案?

在此先感谢,我真的很感谢帮助。除了自己直接修改生成的XML之外,我一直无法找出或找到更好的解决方案。

回答

1

那么我没有看到写出xml-stylesheet处理指令给你写SyndicationFeed到相同的XmlWriter的任何问题。示例代码

SyndicationFeed feed = new SyndicationFeed("Test Feed", "This is a test feed", new Uri("http://http://example.com/testfeed"), "TestFeedID", DateTime.Now); 
    SyndicationItem item = new SyndicationItem("Test Item", "This is the content for Test Item", new Uri("http://example.com/ItemOne"), "TestItemID", DateTime.Now); 

    List<SyndicationItem> items = new List<SyndicationItem>(); 
    items.Add(item); 
    feed.Items = items; 

    using (XmlWriter xw = XmlWriter.Create(Console.Out, new XmlWriterSettings() { Indent = true })) 
    { 
     xw.WriteStartDocument(); 
     xw.WriteProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"sheet.xsl\""); 
     Atom10FeedFormatter atomFormatter = new Atom10FeedFormatter(feed); 
     atomFormatter.WriteTo(xw); 
     xw.Close(); 
    } 

写道

<?xml-stylesheet type="text/xsl" href="sheet.xsl"?> 
<feed xmlns="http://www.w3.org/2005/Atom"> 
    <title type="text">Test Feed</title> 
    <subtitle type="text">This is a test feed</subtitle> 
    <id>TestFeedID</id> 
    <updated>2011-08-02T13:19:12+02:00</updated> 
    <link rel="alternate" href="http://http//example.com/testfeed" /> 
    <entry> 
    <id>TestItemID</id> 
    <title type="text">Test Item</title> 
    <updated>2011-08-02T13:19:12+02:00</updated> 
    <link rel="alternate" href="http://example.com/ItemOne" /> 
    <content type="text">This is the content for Test Item</content> 
    </entry> 
</feed> 

到控制台,你可以写任何目的地的的XmlWriter可以写入相同的方式。

+0

啊,那简单得多。我知道肯定有更好的办法,但现在看来它很明显,现在它已经摆在我面前了。谢谢! – Matt