Question namevaluecollection to html table

darkdusky

Member
Joined
Feb 25, 2009
Messages
8
Programming Experience
3-5
Hi I have a NameValueCollection which I would like to use to build a html table. so I need to loop through it and add <tr> around the rows and <td> around the values.

The collection has forename="Bill", surname="Jones",Address="1 A...."

forename="Tom", surname="Brown",Address="6C...."



I want to build this string:

<table><tr><td>Bill</td><td>Jones</td><td>1 A...</td></tr>

<tr><td>Tom</td><td>Brown</td><td>6C..</td></tr></table>
 
I put together some code that may be helpful, but you may want to consider using something else than a name value collection if you plan to run this on multiple rows:

VB.NET:
Module Module1
    Sub Main()
        Dim nvc As New Collections.Specialized.NameValueCollection()
        nvc.Add("firstname", "first1")
        nvc.Add("lastname", "last1")
        nvc.Add("address", "address1")

        Dim otd As String = "<td>"
        Dim ctd As String = "</td>"
        Dim otr As String = "<tr>"
        Dim ctr As String = "</tr>"
        Dim strHTML As String = "<table>"
        strHTML &= otr
        For Each s As String In nvc
            strHTML &= otd & nvc(s) & ctd
        Next
        strHTML &= ctr
        strHTML &= "</table>"
        Console.WriteLine(strHTML)
    End Sub
End Module
 
Back
Top