Solved

Serialize a structure to xml in VB.NET

Posted on 2008-06-24
3
2,378 Views
Last Modified: 2013-11-26
I'm sure this is really really simple. So simple that I can't find an example of it anywhere.

I have a real simple structure defined in VB.NET. In contains some strings, dates, numbers, and a couple user-defined types. See the code snippet.

How do I serialize this to XML? I know there's some magic I can do that will convert this to and from XML without actually having to WRITE an xml parser... something to do with the "<Serializable()>" tag that I put somewhere...

but how?
''' <summary>

''' Defines header information about this media

''' </summary>

''' <remarks></remarks>

<Serializable()> _

Public Structure MediaProperties

    Dim name As String

    Dim owner As String

    Dim description As String

    Dim storagetype As StorageTypes

    Dim backuptype As BackupTypes

End Structure
 

Public Enum MediaTypes

    sometype1 = 1

    sometype2 = 2

    sometype3 = 3

End Enum
 

Public Enum StorageTypes

    sometype1 = 1

    sometype2 = 2

    sometype3 = 3

End Enum
 

Public Enum BackupTypes

    sometype1 = 1

    sometype2 = 2

    sometype3 = 3

End Enum

Open in new window

0
Comment
Question by:Frosty555
  • 2
3 Comments
 
LVL 10

Accepted Solution

by:
margajet24 earned 500 total points
ID: 21862445

' SERIALIZE
Dim fs As FileStream = New FileStream("serialized.xml", FileMode.Create)
Dim xs As XmlSerializer = New XmlSerializer(GetType( TYPE ) )
xs.Serialize(fs, OBJECT)
fs.Close

' DESERIALIZE
Dim fs As FileStream = New FileStream("serialized.xml", FileMode.Open)
Dim xs As XmlSerializer = New XmlSerializer(GetType( TYPE ) )
Dim obj As TYPE  = CType(xs.Deserialize(fs), TYPE )
fs.Close
0
 
LVL 12

Expert Comment

by:rgn2121
ID: 21862461
Try this out...I have read about it, but never used it much as I didn't realize it was there till I had already written a fucntion to write toi XML...  :(

http://www.java2s.com/Code/VB/XML/XML-Serialize.htm
http://www.devcity.net/Articles/113/1/dotnet_serialization.aspx
0
 
LVL 10

Expert Comment

by:margajet24
ID: 21891084
' SERIALIZE
Dim fs As FileStream = New FileStream("serialized.xml", FileMode.Create)
Dim xs As XmlSerializer = New XmlSerializer(GetType( MediaProperties) )
xs.Serialize(fs, OBJECT)
fs.Close

' DESERIALIZE
Dim fs As FileStream = New FileStream("serialized.xml", FileMode.Open)
Dim xs As XmlSerializer = New XmlSerializer(GetType( MediaProperties) )
Dim obj As MediaProperties= CType(xs.Deserialize(fs), MediaProperties)
fs.Close
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now