Solved

Serialize a structure to xml in VB.NET

Posted on 2008-06-24
3
2,463 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

839 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