473,402 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

xmlserializer and IDIctionary

Hello,

I wrote this code a while back to solve the problem of serializing
hashtables that can hold complex types (as long as you know what types
it may hold ahead of time).

Public Class SerializableHashtable
Inherits System.Collections.Hashtable
Implements IXmlSerializable

Private _EntrySerializer As XmlSerializer

Public Sub New()
Dim HashInfo As System.Reflection.MemberInfo = Me.GetType
Dim InclAttrs As XmlIncludeAttribute() =
CType(HashInfo.GetCustomAttributes(GetType(XmlIncl udeAttribute),
True), XmlIncludeAttribute())
Dim InclTypes(InclAttrs.Length - 1) As Type
For idx As Integer = 0 To InclAttrs.Length - 1
InclTypes(idx) = InclAttrs(idx).Type
Next
_EntrySerializer = New XmlSerializer(GetType(HashEntry),
InclTypes)
End Sub

Public Sub WriteXml(ByVal writer As System.Xml.XmlWriter)
Implements System.Xml.Serialization.IXmlSerializable.WriteXml
For Each key As Object In MyBase.Keys
Dim entry As New HashEntry
entry.Key = key
entry.Value = Me.Item(key)
_EntrySerializer.Serialize(writer, entry)
Next
End Sub

Public Sub ReadXml(ByVal reader As System.Xml.XmlReader)
Implements System.Xml.Serialization.IXmlSerializable.ReadXml
' skip empty node
If reader.IsEmptyElement = True Then
reader.Read()
Return
End If

' skip start tag
reader.Read()
reader.MoveToContent()
While ((Not reader.EOF) AndAlso (reader.Name = "HashEntry"))
Dim entry As HashEntry =
CType(_EntrySerializer.Deserialize(reader), HashEntry)
Me.Add(entry.Key, entry.Value)
reader.MoveToContent()
End While
reader.ReadEndElement()
End Sub

Public Function GetSchema() As System.Xml.Schema.XmlSchema
Implements System.Xml.Serialization.IXmlSerializable.GetSchem a
Return Nothing
End Function

Class HashEntry
Public Key As Object
Public Value As Object
End Class

End Class
To make use of it simply derive your new type from it and mark it with
xmlincludes() for any types it may hold...
<XmlInclude(GetType(SomeClass), XmlInclude(GetType(SomeOtherClass))> _
Public Class ExampleHashTable
Inherits Utility.SerializableHashtable
End Class

Feel free to use/modify for your own purposes. Please share if you can
improve it.
Nov 12 '05 #1
2 5676
Chris -

Hi. I'm pretty much exclusively a C# guy and I'm trying to do something that I thought would be really simple - XMLSerialize a Hashtable. I'm getting a runtime "Reflection" error. The class I'm trying to serialize is nothing but two string members and one hashtable (all Public). And I get this error even when the Hashtable hasn't even been populated.

Ideas?

"Chris Hickman" wrote:
Hello,

I wrote this code a while back to solve the problem of serializing
hashtables that can hold complex types (as long as you know what types
it may hold ahead of time).

Public Class SerializableHashtable
Inherits System.Collections.Hashtable
Implements IXmlSerializable

Private _EntrySerializer As XmlSerializer

Public Sub New()
Dim HashInfo As System.Reflection.MemberInfo = Me.GetType
Dim InclAttrs As XmlIncludeAttribute() =
CType(HashInfo.GetCustomAttributes(GetType(XmlIncl udeAttribute),
True), XmlIncludeAttribute())
Dim InclTypes(InclAttrs.Length - 1) As Type
For idx As Integer = 0 To InclAttrs.Length - 1
InclTypes(idx) = InclAttrs(idx).Type
Next
_EntrySerializer = New XmlSerializer(GetType(HashEntry),
InclTypes)
End Sub

Public Sub WriteXml(ByVal writer As System.Xml.XmlWriter)
Implements System.Xml.Serialization.IXmlSerializable.WriteXml
For Each key As Object In MyBase.Keys
Dim entry As New HashEntry
entry.Key = key
entry.Value = Me.Item(key)
_EntrySerializer.Serialize(writer, entry)
Next
End Sub

Public Sub ReadXml(ByVal reader As System.Xml.XmlReader)
Implements System.Xml.Serialization.IXmlSerializable.ReadXml
' skip empty node
If reader.IsEmptyElement = True Then
reader.Read()
Return
End If

' skip start tag
reader.Read()
reader.MoveToContent()
While ((Not reader.EOF) AndAlso (reader.Name = "HashEntry"))
Dim entry As HashEntry =
CType(_EntrySerializer.Deserialize(reader), HashEntry)
Me.Add(entry.Key, entry.Value)
reader.MoveToContent()
End While
reader.ReadEndElement()
End Sub

Public Function GetSchema() As System.Xml.Schema.XmlSchema
Implements System.Xml.Serialization.IXmlSerializable.GetSchem a
Return Nothing
End Function

Class HashEntry
Public Key As Object
Public Value As Object
End Class

End Class
To make use of it simply derive your new type from it and mark it with
xmlincludes() for any types it may hold...
<XmlInclude(GetType(SomeClass), XmlInclude(GetType(SomeOtherClass))> _
Public Class ExampleHashTable
Inherits Utility.SerializableHashtable
End Class

Feel free to use/modify for your own purposes. Please share if you can
improve it.

Nov 12 '05 #2
Alex Maghen wrote:
Chris -

Hi. I'm pretty much exclusively a C# guy and I'm trying to do something
that I thought would be really simple - XMLSerialize a Hashtable. I'm
getting a runtime "Reflection" error. The class I'm trying to serialize is
nothing but two string members and one hashtable (all Public). And I get
this error even when the Hashtable hasn't even been populated.

Ideas?

"Chris Hickman" wrote:
Hello,

I wrote this code a while back to solve the problem of serializing
hashtables that can hold complex types (as long as you know what types
it may hold ahead of time).

Public Class SerializableHashtable
Inherits System.Collections.Hashtable
Implements IXmlSerializable

Private _EntrySerializer As XmlSerializer

Public Sub New()
Dim HashInfo As System.Reflection.MemberInfo = Me.GetType
Dim InclAttrs As XmlIncludeAttribute() =
CType(HashInfo.GetCustomAttributes(GetType(XmlIncl udeAttribute),
True), XmlIncludeAttribute())
Dim InclTypes(InclAttrs.Length - 1) As Type
For idx As Integer = 0 To InclAttrs.Length - 1
InclTypes(idx) = InclAttrs(idx).Type
Next
_EntrySerializer = New XmlSerializer(GetType(HashEntry),
InclTypes)
End Sub

Public Sub WriteXml(ByVal writer As System.Xml.XmlWriter)
Implements System.Xml.Serialization.IXmlSerializable.WriteXml
For Each key As Object In MyBase.Keys
Dim entry As New HashEntry
entry.Key = key
entry.Value = Me.Item(key)
_EntrySerializer.Serialize(writer, entry)
Next
End Sub

Public Sub ReadXml(ByVal reader As System.Xml.XmlReader)
Implements System.Xml.Serialization.IXmlSerializable.ReadXml
' skip empty node
If reader.IsEmptyElement = True Then
reader.Read()
Return
End If

' skip start tag
reader.Read()
reader.MoveToContent()
While ((Not reader.EOF) AndAlso (reader.Name = "HashEntry"))
Dim entry As HashEntry =
CType(_EntrySerializer.Deserialize(reader), HashEntry)
Me.Add(entry.Key, entry.Value)
reader.MoveToContent()
End While
reader.ReadEndElement()
End Sub

Public Function GetSchema() As System.Xml.Schema.XmlSchema
Implements System.Xml.Serialization.IXmlSerializable.GetSchem a
Return Nothing
End Function

Class HashEntry
Public Key As Object
Public Value As Object
End Class

End Class
To make use of it simply derive your new type from it and mark it with
xmlincludes() for any types it may hold...
<XmlInclude(GetType(SomeClass), XmlInclude(GetType(SomeOtherClass))> _
Public Class ExampleHashTable
Inherits Utility.SerializableHashtable
End Class

Feel free to use/modify for your own purposes. Please share if you can
improve it.

Problem here is that the hashcode are really not 'duplicable', they are
unique. So you must write your own ISerializable and your own deserialize
to build the Hashtable again!
Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stuart Robertson | last post by:
I am trying to find a solution that will allow me to use XmlSerializer to serialize/deserialize a collection of objects where a given object is shared between two or more other objects, and not...
1
by: Bluetears76 | last post by:
Hi I have a hirachy of classes which are Message(base), then FileMessage and ChatMessage (extended) I want to serialize the objects and when i am deserizaling i dont know if i am getting...
3
by: Loui Mercieca | last post by:
Hi, I have created a class, named FormField , which basically contains two fields, name and value. I have set the tag before the class and the field is set as an XmlAttribute whil the name as...
12
by: SJD | last post by:
I've just read Christoph Schittko's article on XmlSerializer: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnxmlnet/html/trblshtxsd.asp . . . and very informative it is too....
4
by: Sanjay Vyas | last post by:
Sorry, forgot to cross post this one.. This is rather unusual as we would expect any Collection class to implement ICollection interface and furthermore a Dictionary class should implement...
4
by: John C | last post by:
I'm new to C#, so just point me at the correct reference material if this question has been answered before. When creating a new class which implements the IDictionary interface, two versions of...
1
by: John | last post by:
I have a collection of properties from a WMI query and I want to recreate the collection and exclude duplicates and then enumerate. In VBScript I used the "Scripting.Dictionary" object and the...
10
by: J055 | last post by:
Hi I have an OrderedDictionary object where the key is an enum. Is there an easy way to cast it to an integer? Examples/document appreciated. Thanks Andrew
1
by: vairamuthu | last post by:
why xmlserializer is not serializing an object which implements IDictionary
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.