serialize class to xml

darkdusky

Member
Joined
Feb 25, 2009
Messages
8
Programming Experience
3-5
I have a class I want to serialize to an XML string. I have tried several different examples on the net but when I debug the class instance is filled with values, but the stringbuilder /writer is "Nothing"

the latest example I tried is this conversion of a C# example:
VB.NET:
Friend Shared Function SerializeObject(Of T)(objectGraph As T) As String
        Dim sb As New StringBuilder()

        Dim writerSettings As New XmlWriterSettings()
        writerSettings.OmitXmlDeclaration = True
        writerSettings.Indent = True

        Using xmlWriter__1 As XmlWriter = XmlWriter.Create(sb, writerSettings)
            Dim xs As New XmlSerializer(GetType(T))
            Dim ns As New XmlSerializerNamespaces()
            ns.Add([String].Empty, [String].Empty)
            xs.Serialize(xmlWriter__1, objectGraph, ns)
        End Using

        Return sb.ToString()
    End Function
I try to use this function like this:
VB.NET:
Private Sub SaveToTable(ByVal strCustomerID As String, ByVal ThisCustomer As Customer)
       
        Dim CustomerXml As String = SerializeObject(ThisCustomer)


        Dim cmd As New SqlCommand
        Dim rowsAffected As Integer

        With cmd
            .CommandType = CommandType.StoredProcedure
            .CommandText = "UPD_Customer"
            .Parameters("@CustomerID").Value = strCustomerID
            .Parameters("@CustomerXML").Value = CustomerXml
        End With


        Using connection As New SqlConnection(DBConnectionString)

            cmd.Connection.Open()
            rowsAffected = cmd.ExecuteNonQuery()
        End Using


    End Sub
And this is my class:
VB.NET:
Imports Microsoft.VisualBasic

Public Class Customer
    Public Property Id() As String
        Get
            Return m_Id
        End Get
        Set(value As String)
            m_Id = value
        End Set
    End Property
    Private m_Id As String

 Public Property Field1() As String
        Get
            Return m_Field1
        End Get
        Set(value As String)
            m_Field1 = value
        End Set
    End Property
    Private m_Field1 As String
Public Property Field2() As String
        Get
            Return m_Field2
        End Get
        Set(value As String)
            m_Field2 = value
        End Set
    End Property
    Private m_Field2 As String

End Class

As I say the class instance is filled but I cannot get it serialized. I've tried loads of examples from net but no success yet so I assume I'm leaving out something.
 
I think you are drawing the wrong conclusions at some point, the code to serialize to xml works fine as is and variable CustomerXml in SaveToTable method will contain that xml string after assigned.
 
Hi, thanks for getting back to me.
I put a breakpoint in the function and checked for values.
in this section:
xs.Serialize(xmlWriter__1, objectGraph, ns)

hovering over objectGraph displayed a filled instance with all values filled, everywhere else on the line is "Nothing"
And on the line:
Return sb.ToString()
sb is also "Nothing"

I've tried loads of very similar examples from the web but can't get any to work. Do I need to modify the Customer class in any way, or to create some kind of XML structure template?
Do I need to do something like this:
<XmlRoot("Unit")> Public Class Something
<XmlAttribute("ID")> Public Property Unit() As String = "100"
<XmlText()> Public Property UnitDesc() As String = "Supervisor Unit"
End Class


http://www.vbdotnetforums.com/xml/51418-xml-serialization.html
 
Last edited:
Can you show you a slightly modified version using other examples? I still have the same problem, i.e. the instance of the class shows values in debugger but the writer /string taking serializer are "Nothing"

Here is my class:

Imports Microsoft.VisualBasic
Imports System.Xml.Serialization
Imports System.IO
Imports System.Xml
Namespace myNamespace

<XmlRoot("Customer")> _
Public Class Customer
Public Sub New()
End Sub 'New
<XmlElement("Name")> _
Public Property Name() As String
Get
Return m_Name
End Get
Set(value As String)
m_Name = value
End Set
End Property
Private m_Name As String
<XmlElement("ID")> _
Public Property ID() As String
Get
Return m_ID
End Get
Set(value As String)
m_ID = value
End Set
End Property

<XmlElement("Field1")> _
Public Property Field1() As String
Get
Return m_Field1
End Get
Set(value As String)
m_Field1 = value
End Set
End Property
Private m_Field1 As String

<XmlElement("Field2")> _
Public Property Field2() As Decimal
Get
Return m_Field2
End Get
Set(value As Decimal)
m_Field2 = value
End Set
End Property
Private m_Field2 As Decimal




Public Shared Function SerializeXML(ByVal obj As Customer) As String
Dim writer As New XmlSerializer(GetType(Customer))
Dim SB As New System.Text.StringBuilder()
Dim XML As New StringWriter(SB)
Try

writer.Serialize(XML, obj)
Return SB.ToString

Catch ex As Exception
Return ""
Finally

XML.Close()
XML.Dispose()

End Try

End Function

Public Function SerializeXML() As String
Return Customer.SerializeXML(Me)
End Function




End Class
End Namespace

and this is how I call it:
Dim CustomerXml As String = myNameSpace.Customer.SerializeXML(thisCustomer)
 
And on the line:
Return sb.ToString()
sb is also "Nothing"
sb refers to the StringBuilder, and that was instantiated using New keyword, it is impossible that sb variable refers to Nothing in that code.
ToString is a method, you can't put a breakpoint on that line and see the return value of the function evaluated each time you hover it, that is not how the debugger works. You can however at that point explicitly tell debugger to evaluate an expression using Immediate Window, enter ?sb.ToString in that window and press Enter and you will see the xml string. That is also not necessary as I explained in my previous post.
 
Thank you for your help. you were right the problem was not with the serializer. I didn't know about the Immediate Window but managed to retrieve an error using try catch and outputting to text file (I'm doing this is a web service so I had problems getting error)
Here is output to text file I used
Writing Text to a File
But I'll try the Immediate Window next time.
Thanks - I was chasing my tail until you made me re-examine what I thought was wrong.
 
Back
Top