Serialize a Class to XML that contains another class with vb.net

I had the 2 classes.  I’m trying to serialize them into XML in an application I was dealing with at GE Healthcare.  Here is the result I’m getting:

<Main>
    <Field1 />
    <Field2 />
</Main>

This is the result I would like to be getting:
<Main>
    <Common>
        <Market />
        <AcctNum />
    </Common>
    <Field1 />
    <Field2 />
</Main>

I didn’t understand what I was missing. Well it turns out I was trying to set two roots with both classes having the XmlRoot attribute. So here is what I came up with. Hope it helps someone.

<Serializable(), XmlRoot("Main")> _
Public Class clsMain
  <XmlElement("Common")> _
  Public Common As New clsCommon()
  Private m_Field1 As String = String.Empty
  <XmlAttribute("field1")> _
  Public Property Field1() As String
    Get
      Return m_Field1
    End Get
    Set(ByVal value As String)
      m_Field1 = value.ToString
    End Set
  End Property
  Private m_Field2 As String = String.Empty
  <XmlAttribute("field2")> _
  Public Property Field2() As String
    Get
      Return m_Field2
    End Get
    Set(ByVal value As String)
      m_Field2 = value.ToString
    End Set
  End Property
  Public Function Serialize() As XmlDocument
    Dim serializer As New XmlSerializer(Me.GetType())
    Dim writer As New StringWriter()
    Dim serializedObject As New XmlDocument()
    serializer.Serialize(writer, Me)
    serializedObject.LoadXml(writer.ToString())
    Return serializedObject
  End Function
  Public Sub Serialize(ByVal fileName As String)
    Dim ns As New XmlSerializerNamespaces()
    ns.Add("", "")
    Dim serializer As New XmlSerializer(Me.GetType())
    Using stream As New FileStream(fileName, FileMode.Create)
      serializer.Serialize(stream, Me, ns)
    End Using
  End Sub
End Class
<Serializable()> _
Public Class clsCommon
  Private m_Market As String = String.Empty
  <XmlAttribute("market")> _
  Public Property Market() As String
    Get
      Return m_Market
    End Get
    Set(ByVal value As String)
      m_Market = value.ToString
    End Set
  End Property
  Private m_AcctNum As String = String.Empty
  <XmlAttribute("acct")> _
  Public Property AcctNum() As String
    Get
      Return m_AcctNum
    End Get
    Set(ByVal value As String)
      m_AcctNum = value.ToString
    End Set
  End Property
End Class

It’s usage would be

Dim c As New clsMain()
    c.Common.AcctNum = "1"
    c.Common.Market = "Industry"
    c.Field1 = "f1"
    c.Field2 = "f2"
    c.Serialize("c:\temp\main.xml")

Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: