Solved

VB.NET Serialize Hashtable (nonbinary)

Posted on 2009-04-15
6
1,101 Views
Last Modified: 2013-11-08
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.
0
Comment
Question by:Craz3d
  • 4
  • 2
6 Comments
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 24155106
Depending on the flavour of XML you want, I have two options....
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 24155109
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

0
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 500 total points
ID: 24155115
2) Create a DataSet, populate it with the HashTable data, then use the DataSet.WriteToXML method...
    Public Shared Function HashToXML(ByVal ht As Hashtable) As String

 

        Dim sb As New Text.StringBuilder

        Dim sr As New IO.StringWriter(sb)

 

        Dim ds As New DataSet("HashToXML")

        Dim dt As New DataTable("Item")

        dt.Columns.Add("Key")

        dt.Columns.Add("Value")

        ds.Tables.Add(dt)

 

        For Each key As Object In ht.Keys

            dt.Rows.Add(New Object() {key.ToString, ht(key).ToString})

        Next

 

        dt.WriteXml(sr)

 

        Return sb.ToString

 

    End Function

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 1

Author Comment

by:Craz3d
ID: 24155324
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?
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 24155420
>>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

0
 
LVL 1

Author Comment

by:Craz3d
ID: 24155560
Thank you webtubbs.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now