all groups > dotnet xml > february 2007 >
You're in the

dotnet xml

group:

XML Serialization with of a sub-class of DataTable.


XML Serialization with of a sub-class of DataTable. sylvain.ross NO[at]SPAM gmail.com
2/22/2007 12:25:21 PM
dotnet xml: Hello everybody,

I have a very weird problem regarding the serialization of a class
that I wrote (named DummyClass in my exemple).
My class inherits from DataTable.

Then I just try a dummy XMLSerialization and then Deserialize it into
a new object.

The problem is that everything is fine for the DataTable containt, but
nothing of my custom properties added to the DummyClass are serialized
properly !

I don't understand why the serialization process works perfectly, but
when deserializing, it just skips the custom properties and just take
care of the data table content ? why ?

Thanks in advanced !


Here is my code :

==============================================

<Serializable()> _
Public Class DummyClass
Inherits DataTable

Private _Name As String
Private _Description As String
Private _Something As String

Public Shared Sub Main()

Dim fileToUse As String = "C:\foo.xml"

' Write the instance to the file
Dim dummyInstance As New DummyClass()
dummyInstance.TableName = "SomeName"
dummyInstance.Name = "TheName"
dummyInstance.Description = "TheDescription"
dummyInstance.Something = "Something He"

Dim serializer As New
Xml.Serialization.XmlSerializer(GetType(DummyClass))
Dim stream As New IO.StreamWriter(fileToUse)
serializer.Serialize(stream, dummyInstance)
stream.Close()

Dim stream2 As New IO.StreamReader(fileToUse)
Dim dummyInstance2 As DummyClass =
DirectCast(serializer.Deserialize(stream2), DummyClass)
stream2.Close()

MsgBox(dummyInstance2.Something & " == " &
dummyInstance.Something & " ?")

End Sub

Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal Value As String)
_Name = Value
End Set
End Property


Public Property Description() As String
Get
Return _Description
End Get
Set(ByVal Value As String)
_Description = Value
End Set
End Property

Public Property Something() As String
Get
Return _Something
End Get
Set(ByVal Value As String)
_Something = Value
End Set
End Property

End Class

===============================================
Re: XML Serialization with of a sub-class of DataTable. Keith Patrick
2/22/2007 3:33:49 PM
Try tagging the new properties with XmlAttributeAttribute. Simple types,
like strings, enums, and ints that are represented as attributes need the
flag, but complex types do not need XmlElementAttribute




Re: XML Serialization with of a sub-class of DataTable. sylvain.ross NO[at]SPAM gmail.com
2/23/2007 5:06:59 AM
I tried all kinds of Attributes but nonee of them worked, nothing
changes the XML with all my attributes.

I suspect that the DataTable serialization process overrides the
serialization of the whole class and just ignore my fields.
This is really weird and besides getting rid of my inheritance of the
DataTable class (which is not a valid solution) I dont know wwhat I
can do to go around this problem !

Any idea about what is happening ?


Thanks in advance.
Sylvain

On Feb 22, 10:33 pm, "Keith Patrick"
[quoted text, click to view]

AddThis Social Bookmark Button