We help IT Professionals succeed at work.
Get Started

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

5,422 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:
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE