?
Solved

Serialize a structure to xml in VB.NET

Posted on 2008-06-24
3
Medium Priority
?
2,629 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

762 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