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:
I try to use this function like this:
And this is my 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.
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
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
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.