Hashtable speichern?

26/09/2007 - 21:27 von dd | Report spam
Hallo,

gibt es irgendeine Möglichkeit einen Hashtable in einer Datei zu speichern?

Ich habe es bereits mit dem XmlSerializer versucht, aber der kommt damit
nicht klar.
 

Lesen sie die antworten

#1 Peter Fleischer
26/09/2007 - 21:43 | Warnen spam
dd wrote:

gibt es irgendeine Möglichkeit einen Hashtable in einer Datei zu
speichern?
Ich habe es bereits mit dem XmlSerializer versucht, aber der kommt
damit nicht klar.



Imports System
Imports System.IO
Imports System.Collections
Imports System.Xml.Serialization
Public Module Module1
Public Sub Main()
Dim ht As New Hashtable
ht.Add("1", New C1(123, "a"))
ht.Add("3", New C1(345, "b"))
ht.Add("2", New C1(234, "c"))
SerializeCollection("c:\temp\x.xml", ht)
End Sub
Private Sub SerializeCollection(ByVal filename As String, ByVal d As
IDictionary)
Dim c As New C2(d)
Dim x As XmlSerializer = New XmlSerializer(GetType(C2))
Dim writer As TextWriter = New StreamWriter(filename)
x.Serialize(writer, c)
writer.Close()
End Sub
End Module
<Serializable()> Public Class C1
Public V1 As Integer
Public V2 As String
Public Sub New()
End Sub
Public Sub New(ByVal v1 As Integer, ByVal v2 As String)
Me.V1 = v1
Me.V2 = V2
End Sub
End Class
Public Class C2
Implements ICollection
Public CollectionName As String
Private arr As ArrayList = New ArrayList()
Private l As IDictionary
Public Sub New(ByVal d As IDictionary)
l = d
End Sub
Default Public Overloads ReadOnly _
Property Item(ByVal index As Integer) As C3
Get
Dim en1 As IEnumerator = l.Keys.GetEnumerator
Dim en2 As IEnumerator = l.Values.GetEnumerator
Dim i As Integer
For i = 0 To index
en1.MoveNext()
en2.MoveNext()
Next
Return New C3(en1.Current.ToString, CType(en2.Current, C1))
End Get
End Property
Public Sub CopyTo(ByVal a As Array, ByVal index As Integer) _
Implements ICollection.CopyTo
l.CopyTo(a, index)
End Sub
Public ReadOnly Property Count() As Integer Implements _
ICollection.Count
Get
Count = l.Count
End Get
End Property
Public ReadOnly Property SyncRoot() As Object _
Implements ICollection.SyncRoot
Get
Return Me
End Get
End Property
Public ReadOnly Property IsSynchronized() As Boolean _
Implements ICollection.IsSynchronized
Get
Return False
End Get
End Property

Public Function GetEnumerator() As IEnumerator _
Implements IEnumerable.GetEnumerator
Return l.GetEnumerator()
End Function
Public Function Add(ByVal newc As C3) As Integer
l.Add(newc.k, newc.v)
Return l.Count
End Function
End Class
<Serializable()> Public Class C3
Public Sub New()
End Sub
Public Sub New(ByVal key As String, ByVal v As C1)
Me.k = key
Me.v = v
End Sub
<System.Xml.Serialization.XmlAttributeAttribute("Key")> Public k As String
Public v As C1
End Class

Du erhöhst Deine Chance, dass Deine Frage überhaupt gelesen und qualifiziert
und schnell beantwortet wird, wenn Du Deinen Realnamen im Absender
einstellst (Vor- und Zuname).

Viele Grüße

Peter

Ähnliche fragen