We help IT Professionals succeed at work.

Creating RSS Feed in VB.net, How to add line breaks to output

Medium Priority
5,375 Views
Last Modified: 2013-11-18
I am using vb.net (vs 2008) to create an RSS feed from SQL Server 2008 database.

I have been successful reading the data from the db and writing to an RSS feed. I would like help formating the RSS feed.

Each record that I pull from the data base contains 5 fields. I am usingthe first field as the title of the item on the RSS feed. I would like each field to be written on a new line, but I have not been able to get it to do anything but print all the fields in the same line.

I have attached the code I am using to create the RSS feed. In the code I included comments where I would like a new line to be inserted in the output.

I have also attached one image showing how the output looks now and a second image of output from a similar RSS feed that I would like my feed to look like.

Thanks any help you can provide.



 This is the output I am getting now This is similar to how I want the output to look.
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        'send document as response      
        context.Response.ContentType = "text/xml"
        context.Response.ContentEncoding = System.Text.Encoding.UTF8
        context.Response.Cache.SetExpires(DateTime.Now.AddSeconds(3))
        context.Response.Cache.SetCacheability(HttpCacheability.Public)
        

        'create RSS xml document 
        Dim xml As New XmlTextWriter(context.Response.OutputStream, Encoding.UTF8)
        xml.Formatting = Formatting.Indented
        xml.WriteStartDocument()
        xml.WriteStartElement("rss")
        xml.WriteAttributeString("version", "2.0")
        'create feed header section xml.WriteStartElement("channel") 
        xml.WriteStartElement("channel")
        xml.WriteElementString("title", "Data Feed")
        xml.WriteElementString("link", "http://www.website.com")
        xml.WriteElementString("description", "View last 90 days of data for the selected asset.")
        xml.WriteElementString("language", "en-us")
        xml.WriteElementString("pubDate", DateTime.UtcNow.ToString("r"))
        xml.WriteElementString("lastBuildDate", DateTime.UtcNow.ToString("r"))
        xml.WriteElementString("managingEditor", "john@website.com")
        xml.WriteElementString("webMaster", "john@website.com")
        xml.WriteElementString("ttl", "1")

        'get data rows
        Dim ds As DataSet = GetData()
        
        'add rows to feed 
        Dim row As DataRow
        For Each row In ds.Tables(0).Rows
            
            xml.WriteStartElement("item")
            xml.WriteElementString("title", row("PointOfInterestDescription"))
            xml.WriteElementString("link", "www.website.com")
            'output rows of data 

            xml.WriteStartElement("description")
            xml.WriteElementString("description", "PointOfInterestCode:  ")
            xml.WriteElementString("description", row("PointOfInterestCode"))
            'Would like new line here
            xml.WriteElementString("description", "ObservationTime:  ")
            xml.WriteElementString("description", CType(row("ObservationTime"), DateTime).ToString("G"))
            'Would like new line here
            xml.WriteElementString("description", "ObservationValue:  ")
            xml.WriteElementString("description", row("ObservationValue"))
            'Would like new line here
            xml.WriteElementString("description", "EmployeeCode:  ")
            xml.WriteElementString("description", row("EmployeeCode"))
            
            xml.WriteEndElement()
            
            xml.WriteElementString("pubDate", CType(row("ObservationTime"), DateTime).ToString("G"))
            xml.WriteEndElement()

        Next
        
        'close channel     
        xml.WriteEndElement()
        'close rss      
        xml.WriteEndElement()
        'close document      
        xml.WriteEndDocument()
        xml.Flush()
        xml.Close()
        
    End Sub

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
Have you tried inserting <br /> tags in your feed?

Author

Commented:
How would I do that?
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:

Author

Commented:
I built a string and inserted "<br />" in the string whereever I wanted a line break. I then used xml.WriteCData(outputstring) instead of WriteElementString.

Thanks

Explore More ContentExplore courses, solutions, and other research materials related to this topic.