2011-08-02 2 views
1

.NET SyndicationFeed 클래스를 사용하여 소비 할 RSS 피드를 만들었지 만 XSLT 스타일 시트를 결과 XML에 연결해야 웹 브라우저의 결과 스타일을 지정할 수 있습니다. 나는 이것을하기위한 쉬운 방법을 찾을 수 없었다.C# .NET SyndicationFeed XSLT에서 XHTML

피드가 생성 된 후 결과 XML에 직접 <?xml-stylesheet ... ?> 태그를 삽입하는 것이 가장 좋습니다. 아니면 더 우아한 해결책이 있습니까?

미리 감사드립니다. 정말 감사드립니다. 필자는 결과 XML을 직접 수정하는 대신 더 나은 솔루션을 찾아 낼 수 없었습니다.

답변

1

글쎄 SyndicationFeed를 쓰는 동일한 XmlWriter에 xml-stylesheet 처리 명령을 쓰는 데 문제가 없다고 예를 들어 보겠습니다. 샘플 코드

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

는 콘솔과 당신은 XmlWriter를가 쓸 수있는 모든 대상에 쓸 수 같은 방법으로

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

를 기록합니다.

+0

아, 훨씬 간단합니다. 나는 더 좋은 방법이 있었음에 틀림 없다는 것을 알았지 만, 이것이 내 앞에 놓여 졌음이 분명해 보인다. 감사! – Matt

관련 문제