Serialization Help

G

Guest

Hi,

I'm having trouble getting XML serialization to work correctly -- I'd
appreciate any help.

To learn this I've got a class I'm going to serialize:

Public Class SerializeThis1
Dim mName As String
Dim mBDay As Date

Public Property Name() As String
Get
Return mName
End Get
Set(ByVal Value As String)
mName = Value
End Set
End Property

Public Property BDay() As Date
Get
Return mBDay
End Get
Set(ByVal Value As Date)
mBDay = Value
End Set
End Property
End Class

I try to serialize it like this:


Imports System.IO
Imports System.Xml.Serialization

Public Class Form1
Inherits System.Windows.Forms.Form

Private Sub uxSave_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles uxSave.Click
Dim mClass As New SerializeThis1
Dim serializer As XmlSerializer
serializer = New XmlSerializer(GetType(SerializeThis1))
mClass.Name = Me.uxName.Text
mClass.BDay = Me.uxBDay.Text
Dim writer As TextWriter
writer = New StreamWriter("SerializationTest.xml")
serializer.Serialize(writer, mClass)
MessageBox.Show("done")
writer.Close()
End Sub
End Class

I wind up with this error:


An unhandled exception of type 'System.IO.FileNotFoundException' occurred
in mscorlib.dll

Additional information: File or assembly name 2ada-ph5.dll, or one of its
dependencies, was not found.

The editor has the following line highlighted:

serializer = New XmlSerializer(GetType(SerializeThis1))

If I try to use my instantiation:
serializer = New XmlSerializer(GetType(mClass))
I wind up with a message (as I'm typing) saying mClass isn't defined.

Obviously I'm not understanding a whole bunch of things. I've tried looking
at examples, and going through help, but I seem to be getting nowhere fast.

I'd appreciate any help.

Thanks,

Art
 
W

wooster11

Art,
I do quite a bit of XML serialization, but haven't done it via a
streamwriter before, rather I use a MemoryStream. But I do handle a
bunch of XML objects. Try this serialization code. This will return a
string instead, but you could easily save it out however you want
(either through a StreamWriter or XMLDocument object). I've also
included a Deserialization method in the event you want turn the XML
into an object. If this still doesn't work, try to use the XML
Attributes to let the serializer know that your properties are XML
Elements. (ie. <xmlElement()> Public Property Blah Blah...

Public Shared Function SerializeToXml(ByRef myObj As
SerializeThis1) As String
Dim memStream As System.IO.MemoryStream
Dim utf8 As New System.Text.UTF8Encoding
Dim xmlDoc As System.Xml.XmlDocument
Dim tmpSerializer As XmlSerializer

Try
'/read and convert to byte array
tmpSerializer = New XmlSerializer(GetType(SerializeThis1))
memStream = New System.IO.MemoryStream
tmpSerializer.Serialize(memStream, myObj)
'Load an xml document up with the serialized xml
xmlDoc = New System.Xml.XmlDocument
xmlDoc.LoadXml(utf8.GetString(memStream.ToArray()))
'Remove the ugly attributes from the root node of the XML
CType(xmlDoc.LastChild,
System.Xml.XmlElement).RemoveAttribute("xmlns:xsd")
CType(xmlDoc.LastChild,
System.Xml.XmlElement).RemoveAttribute("xmlns:xsi")
Catch ex As Exception
Trace.WriteLine("Unable to serialize object to XML: " &
ex.Message & vbCrLf & ex.StackTrace)
Throw New Exception("Unable to serialize object to XML: " &
ex.Message & vbCrLf & ex.StackTrace)
End Try
'return the xml string
Return xmlDoc.OuterXml
End Function

Public Shared Function DeserializeXml(ByVal myObjXml As String) As
SerializeThis1
Dim retObj As SerializeThis1
Dim errMsg As System.Text.StringBuilder
Dim bytar() As Byte
Dim memstream As System.IO.MemoryStream
Dim deserializer As XmlSerializer

'Deserialize the object
Try
bytar = System.Text.UnicodeEncoding.UTF8.GetBytes(myObjXml)
memstream = New System.IO.MemoryStream(bytar)
bytar = Nothing
'/deserialize object
deserializer = New XmlSerializer(GetType(SerializeThis1))
retObj = CType(deserializer.Deserialize(memstream),
SerializeThis1)
Catch ex As Exception
Trace.WriteLine("Unable to deserialize XML into an object:
" & ex.Message & vbCrLf & ex.StackTrace)
Throw New Exception("Unable to deserialize XML into an
object: " & ex.Message & vbCrLf & ex.StackTrace)
End Try

Return retObj
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top