Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / February 2008

Tip: Looking for answers? Try searching our database.

xslt and System.ServiceModel.Syndication

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Paul - 05 Feb 2008 07:23 GMT
Hi,

I am using System.ServiceModel.Syndication to create RSS feeds. I do not
know how to specify which XSL Stylesheet to associate with the feed. If you
build the XML doc "by hand" asp 2 style, you can add a line like this to the
top of the xml doc:
<?xml-stylesheet type="text/xsl" href="urlToXSLstyleSheet" version="1.0"?>

No I use code like the following to emit:

(the "feed" variable in the code is a
System.ServiceModel.Syndication.SyndicationFeed object)

Dim rssWriter As XmlWriter = New XmlTextWriter(Response.OutputStream,
Encoding.UTF8)
       Dim rssFormatter As Rss20FeedFormatter = New Rss20FeedFormatter(feed)
       rssFormatter.WriteTo(rssWriter)
       rssWriter.Flush()
       Response.ContentType = "text/xml"
       Response.End()

My question: how do I specify a XSL Styelsheet for the
System.ServiceModel.Syndication.SyndicationFeed?

Thanks
Paul
Steven Cheng[MSFT] - 05 Feb 2008 09:52 GMT
Hi Paul,

As for the rss transforming issue, I think you may consider the following
approach:

Since you originally write the Syndication object into XMLWriter and flush
out to ASP.NET page response, you can consider first write the RSS xml into
a memoryStream(wrappered through a XmlWriter). Then, you can use .NET XSLT
api to perform tranformation on the XML document in the memorystream(read
it through a StreamReader).  

Here are some information about how to use XSLT transformation classes in
.NET 2.0:

#XSLT Transformations
http://msdn2.microsoft.com/en-us/library/14689742.aspx

#XSL Transformations in .NET 2.0
http://www.15seconds.com/issue/060608.htm

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================
   

This posting is provided "AS IS" with no warranties, and confers no rights.

                                                       

--------------------
>From: =?Utf-8?B?UGF1bA==?= <vis@nospam.nospam>
>Subject: xslt and System.ServiceModel.Syndication
>Date: Mon, 4 Feb 2008 23:23:00 -0800

>Hi,
>
[quoted text clipped - 22 lines]
>Thanks
>Paul
Paul - 05 Feb 2008 12:17 GMT
Steven,

Thanks for your input. I have tried your approach, and find that I get
errors on the transform. I have tried with various data sources, like the
names of folders in my music collecection etc. I get errors like:

"'<', hexadecimal value 0x3C, is an invalid attribute character. Line 1,
position ..."
or
"There is an unclosed literal string ...

How do I deal with these errors without cancelling the whole transform?

Thanks
Paul

> Hi Paul,
>
[quoted text clipped - 87 lines]
> >Thanks
> >Paul
Steven Cheng[MSFT] - 07 Feb 2008 02:24 GMT
Thanks for your reply Paul,

Regarding on the new error message you mentioned, there seems exist some
certain character that make the XSLT transformor complain about. I suggest
you first save the RSS XML document(get from the Syndication object) into a
xml file and then open it in IE to see whether IE will detect any well-form
validation error. You can also try using a simple RSS collection so that
you can also open the saved XML document and check it in some text editor.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.

                                                       

--------------------
>Thread-Topic: xslt and System.ServiceModel.Syndication
>thread-index: Achn8QRDXF8dmdNBRpuVMxLsXmxj0A==
>Subject: RE: xslt and System.ServiceModel.Syndication
>Date: Tue, 5 Feb 2008 04:17:03 -0800

>Steven,
>
[quoted text clipped - 45 lines]
>>
>> Get notification to my posts through email? Please refer to

http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
>> ications.
>>
[quoted text clipped - 53 lines]
>> >Thanks
>> >Paul
Paul - 11 Feb 2008 10:53 GMT
Hi guys,

Thanks for the input. My problems were in the underlying memory streams,
sorry but I can't recall the detail now.

Here is the final method I used to emit from my aspx page:

Private Sub WriteOutput()
       Dim feed As SyndicationFeed = New SyndicationFeed
       feed = GetARSFeed()
       'feed = getDummyFeed()

       Dim Title As TextSyndicationContent = New
TextSyndicationContent(_FeedInfo.FeedName)
       Dim Description As TextSyndicationContent = New
TextSyndicationContent(_FeedInfo.FeedDescription)
       feed.Title = Title
       feed.Description = Description

       Select Case _FeedInfo.OutputFormat
           Case "Raw RSS"
               Dim memstream As New MemoryStream
               Dim rssWriter As XmlWriter = XmlWriter.Create(memstream)
               Dim rssFormatter As Rss20FeedFormatter = New
Rss20FeedFormatter(feed)
               rssFormatter.WriteTo(rssWriter)

               rssWriter.Flush()

               memstream.Seek(0, SeekOrigin.Begin)
               Dim xslReader As XmlReader = XmlReader.Create(memstream)

               Response.ContentType = "text/xml"
               Dim r As New StreamReader(memstream)
               memstream.Seek(0, SeekOrigin.Begin)
               Response.Write(r.ReadToEnd)
               Response.End()
           Case "Raw ATOM"
               Dim memstream As New MemoryStream
               Dim rssWriter As XmlWriter = XmlWriter.Create(memstream)
               Dim atomFormatter As Atom10FeedFormatter = New
Atom10FeedFormatter(feed)
               atomFormatter.WriteTo(rssWriter)

               rssWriter.Flush()

               memstream.Seek(0, SeekOrigin.Begin)
               Dim xslReader As XmlReader = XmlReader.Create(memstream)

               Response.ContentType = "text/xml"
               Dim r As New StreamReader(memstream)
               memstream.Seek(0, SeekOrigin.Begin)
               Response.Write(r.ReadToEnd)
               Response.End()
           Case "RSS Presented as Formatted HTTP"
               Dim memstream As New MemoryStream
               Dim rssWriter As XmlWriter = XmlWriter.Create(memstream)
               Dim rssFormatter As Rss20FeedFormatter = New
Rss20FeedFormatter(feed)
               rssFormatter.WriteTo(rssWriter)

               rssWriter.Flush()

               Dim xslt As New Xsl.XslCompiledTransform()
               xslt.Load(_FeedInfo.XSLTFileName)

               memstream.Seek(0, SeekOrigin.Begin)
               Dim xslReader As XmlReader = XmlReader.Create(memstream)

               Dim newmem As New MemoryStream
               xslt.Transform(xslReader, Nothing, newmem)

               Response.ContentType = "text/HTML"
               Dim r As New StreamReader(newmem)
               newmem.Seek(0, SeekOrigin.Begin)
               Response.Write(r.ReadToEnd)
               Response.End()
       End Select

> Thanks for your reply Paul,
>
[quoted text clipped - 145 lines]
> >> >Thanks
> >> >Paul
Steven Cheng[MSFT] - 12 Feb 2008 03:06 GMT
Thanks for your followup.

I'm glad that you've got it working and also thanks for your sharing.

Have a nice day.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
>From: =?Utf-8?B?UGF1bA==?= <vis@nospam.nospam>
>Subject: RE: xslt and System.ServiceModel.Syndication
>Date: Mon, 11 Feb 2008 02:53:03 -0800

>Hi guys,
>
[quoted text clipped - 155 lines]
>> >>
>> >> Get notification to my posts through email? Please refer to

http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
>> >> ications.
>> >>
[quoted text clipped - 63 lines]
>> >> >Thanks
>> >> >Paul
Martin Honnen - 05 Feb 2008 14:43 GMT
> Hi,
>
[quoted text clipped - 19 lines]
> My question: how do I specify a XSL Styelsheet for the
> System.ServiceModel.Syndication.SyndicationFeed?

Implement your own XmlWriter that writes out the xml-stylesheet
processing instruction. You can do that by subclassing XmlTextWriter for
instance.

Here is an example:

    Public Class XmlStylesheetWriter : Inherits XmlTextWriter
        Private written As Boolean
        Private _type As String = "text/xsl"
        Public Property Type() As String
            Get
                Return _type
            End Get
            Set(ByVal value As String)
                _type = value
            End Set
        End Property

        Private _href As String
        Public Property Href() As String
            Get
                Return _href
            End Get
            Set(ByVal value As String)
                _href = value
            End Set
        End Property

        Public Sub New(ByVal w As System.IO.Stream, ByVal encoding As
System.Text.Encoding)
            MyBase.New(w, encoding)
        End Sub

        Public Sub New(ByVal filename As String, ByVal encoding As
System.Text.Encoding)
            MyBase.New(filename, encoding)
        End Sub

        Public Sub New(ByVal w As System.IO.TextWriter)
            MyBase.New(w)
        End Sub

        Public Overrides Sub WriteStartDocument()
            MyBase.WriteStartDocument()
            WriteXmlStylesheetPI()
        End Sub

        Public Overrides Sub WriteStartDocument(ByVal standalone As
Boolean)
            MyBase.WriteStartDocument(standalone)
            WriteXmlStylesheetPI()
        End Sub

        Public Overrides Sub WriteStartElement(ByVal prefix As String,
ByVal localName As String, ByVal ns As String)
            If Not (written) Then
                WriteXmlStylesheetPI()
            End If
            MyBase.WriteStartElement(prefix, localName, ns)
        End Sub

        Private Sub WriteXmlStylesheetPI()
            MyBase.WriteProcessingInstruction("xml-stylesheet",
String.Format("type=""{0}"" href=""{1}""", Type, Href))
            written = True
        End Sub
    End Class

Used as follows:

        Dim feed As New SyndicationFeed("Example feed", "This is an
example feed", New Uri("http://example.com/"))
        Dim writer As New XmlStylesheetWriter(Console.Out)
        writer.Href = "sheet.xsl"
        writer.Formatting = Formatting.Indented

        Dim formatter = feed.GetRss20Formatter(False)

        formatter.WriteTo(writer)
        writer.Close()

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.