Solved

Serialize a structure to xml in VB.NET

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

749 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