开发者

XSLT to XHTML for a C# .NET SyndicationFeed

开发者 https://www.devze.com 2023-03-24 05:49 出处:网络
I am using the .NET SyndicationFeed class to create an RSS feed for consumption, but I need to link an XSLT style sheet to the resulting XML to style the results for web browsers. I haven\'t been able

I am using the .NET SyndicationFeed class to create an RSS feed for consumption, but I need to link an XSLT style sheet to the resulting XML to style the results for web browsers. I haven't been able to find an easy way of doing this.

Is my best bet to insert the <?xml-stylesheet ... ?> tag into the result开发者_如何学Pythoning XML myself after the feed has been generated, or is there a more elegant solution?

Thanks in advance, I really appreciate the help. I haven't been able to figure out or find a better solution save for directly modifying the resulting XML myself.


Well I don't see any problems writing out the xml-stylesheet processing instruction to the same XmlWriter you write the SyndicationFeed to e.g. the sample code

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

writes

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

to the console and the same way you could write to any destination an XmlWriter can write to.

0

精彩评论

暂无评论...
验证码 换一张
取 消