Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / February 2007

Tip: Looking for answers? Try searching our database.

XML Serialization with of a sub-class of DataTable.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
sylvain.ross@gmail.com - 22 Feb 2007 20:25 GMT
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

===============================================
Keith Patrick - 22 Feb 2007 21:33 GMT
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
sylvain.ross@gmail.com - 23 Feb 2007 13:06 GMT
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"
<richard_keith_patr...@nospam.hotmail.com> wrote:
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.