jdross jdross - 6 months ago 17
Vb.net Question

How to serialize an object into XML

How can I serialize a .net object into XML and then de-serialize it back?

Answer

In VB.Net you would first add some attributes to your class (if needed)

Public Class Class1

    <XmlAttribute("value")>
    Public Property Value As String

    <XmlElement("name")>
    Public Property Name As String

End Class

And then serialize it using

Dim p As New Class1()
p.Name = "test"
Dim sw1 = New StringWriter()
Dim xs1 As New XmlSerializer(GetType(Class1))
xs1.Serialize(New XmlTextWriter(sw1), p)
Console.WriteLine(sw1.ToString())