Solved

Serialize a structure to xml in VB.NET

Posted on 2008-06-24
3
2,537 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

734 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