Link to home
Start Free TrialLog in
Avatar of Craz3d
Craz3dFlag for United States of America

asked on

VB.NET Serialize Hashtable (nonbinary)

I'm very new to VB. Only been using it for about a week. I've got about 9 years of Perl behind me so I can understand concepts perfectly, I just don't know the methods for VB or the 'lingo', be gentle lol.

I've got a basic hash setup something like key: blahblah, value: 8752.  I'm trying to figure out how to drop that out to human-readable .xml. (The data was originally .INI, if that helps at all) Keys are always strings, values are always integers, no chance of any duplicates, ever.

Any help is greatly appreciated as my friend's comment was waay out of my league, creating classes with properties and whatnot. Sounds like rolling my own form of a Hashtable.
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

Depending on the flavour of XML you want, I have two options....
Use the XmlSerializer object....
    Public Shared Function HashToXml(ByVal hash As Hashtable) As String
 
        Dim entries As New List(Of Entry)(hash.Count)
        For Each key As Object In hash.Keys
            entries.Add(New Entry(key, hash(key)))
        Next
 
        Dim sb As New Text.StringBuilder
        Dim writer As New System.IO.StringWriter(sb)
        Dim serializer As New XmlSerializer(GetType(List(Of Entry)))
        serializer.Serialize(writer, entries)
 
        Return sb.ToString
 
    End Function
 
    Public Class Entry
        Public Key As Object
        Public Value As Object
        Public Sub New()
            'nothing here
        End Sub
        Public Sub New(ByVal key As Object, ByVal value As Object)
            Me.Key = key
            Me.Value = value
        End Sub
    End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Craz3d

ASKER

Tried both methods, the first was a bit more obfuscated than I'd like. The second didn't actually put out any newlines in the file but (unless I'm mistaken) I can just do that by hand, it's no biggy.

How might I pull them back into a hashtable?
>>The second didn't actually put out any newlines in the file

In fairness, XML files are normally not meant for viewing directly, but if you do, use the default program designated for opening XML files (which is normally Internet Explorer, although any browser will do). Doing so will present the XML in an easy to read format - with line breaks.

To read the XML back to a HashTable, you can use this function....
    Public Function XMLtoHashTable(ByVal xmlStr As String) As Hashtable
 
        Dim hash As New Hashtable
        Dim sr As New IO.StringReader(xmlStr)
        Dim ds As New DataSet
        ds.ReadXml(sr)
        sr.Close()
        sr.Dispose()
        For Each dr As DataRow In ds.Tables("Item").Rows
            hash.Add(dr("Key"), dr("Value"))
        Next
        ds.Dispose()
        Return hash
 
    End Function

Open in new window

Avatar of Craz3d

ASKER

Thank you webtubbs.