|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionBy default, Generics Collections could not be serialize from xmlserializer. I try to create a generic collection that can serialize in xml format. Public Class CollectionBase(Of T)
Inherits System.Collections.ObjectModel.Collection(Of T)
Implements System.Xml.Serialization.IXmlSerializable
About This CollectionWhen we want to made XmlSerializable object we must Implement it from IXmlSerializable. and then we must implement 3 methods (GetSchema,ReadXml,WriteXml). In GetSchema we must return a string that contain XML Schema about this object (We Leave it blank in this version!). ReadXml method will contain statements that deserialize Xml to Objects and finaly in WriteXml we serialize our Collection into Xml.
Public Sub ReadXml(ByVal reader As System.Xml.XmlReader) _
Implements System.Xml.Serialization.IXmlSerializable.ReadXml
Dim pc() As PropertyInfo = GetType(T).GetProperties()
While reader.Read()
If reader.Name = GetType(T).Name Then
reader.Read()
If pc.Length > 0 Then
Dim ti As T = GetInstance()
For i As Int32 = 0 To pc.Length - 1
If pc(i).CanRead And pc(i).CanWrite Then
Dim st As SerilalizeType = GetSerilalizeType(pc(i).PropertyType)
If st = SerilalizeType.Complex Then
Dim o As Object = GetInstance(pc(i).PropertyType)
DesrializeObject(reader, o, pc(i).Name)
pc(i).SetValue(ti, _
Convert.ChangeType(o, pc(i).PropertyType), _
Nothing)
ElseIf st = SerilalizeType.Guid Then
Dim strGuid As String = reader.ReadElementString(pc(i).Name)
Dim newId As Guid = New Guid(strGuid)
pc(i).SetValue(ti, New Guid(strGuid), Nothing)
ElseIf st = SerilalizeType.Array Then
Dim o As Object = Nothing
DesrializeArray(reader, o, pc(i).PropertyType)
pc(i).SetValue(ti, _
Convert.ChangeType(o, pc(i).PropertyType), _
Nothing)
ElseIf st = SerilalizeType.ICollection Then
Dim o As Object = Nothing
DesrializeCollection(reader, o, pc(i).PropertyType)
pc(i).SetValue(ti, _
Convert.ChangeType(o, pc(i).PropertyType), _
Nothing)
Else
pc(i).SetValue(ti, _
Convert.ChangeType( _
reader.ReadElementString(pc(i).Name), pc(i).PropertyType), _
Nothing)
End If
End If
Next
Me.Add(ti)
End If
End If
End While
End Sub
Public Sub WriteXml(ByVal writer As System.Xml.XmlWriter) _
Implements System.Xml.Serialization.IXmlSerializable.WriteXml
Dim pc() As PropertyInfo = GetType(T).GetProperties()
Dim ti As T = Nothing
For i As Int32 = 0 To Me.Items.Count - 1
ti = Me.Item(i)
writer.WriteStartElement(GetType(T).Name)
For j As Int32 = 0 To pc.Length - 1
If pc(j).CanRead And pc(j).CanWrite Then
writer.WriteStartElement(pc(j).Name)
Dim st As SerilalizeType = GetSerilalizeType(pc(j).PropertyType)
If st = SerilalizeType.Complex Or _
st = SerilalizeType.Array Or _
st = SerilalizeType.ICollection Then
writer.WriteRaw(SerializeObject(pc(j).GetValue(ti, Nothing)))
Else
writer.WriteString(pc(j).GetValue(ti, Nothing).ToString())
End If
writer.WriteEndElement()
End If
Next
writer.WriteEndElement()
Next
End Sub
Using the codeAs normaly use of Generic Collections :
Dim MyCol As New DNE.Components.CollectionBase(Of SomeObject)
I suggest that inherit from this
class. Our collection can now deserialize simple properties like string,integer and Arrays and Obects that inherits from ICollection.
ToDoSupport serialization for Hashtables, Enums and .... HistoryVersion 0.2 : Support for Array and ICollection, Fixed some bugs. 28/11/2007 Version 0.1 : Initial Release 11/11/2007
|
|||||||||||||||||||||||||||||||||||