Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Serialization Question

From: "Terry Holland" <nospam@------.--.-->
To: NULL
Date: 2/15/2009 7:56:00 PM
When I call the SerializeObject method in Test.clsPerson the following XML 
is saved to file

<?xml version="1.0" encoding="utf-8"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Name>John Smith</Name>
  <Address>
    <PostCode>WC1 1AA</PostCode>
    <Address>
      <Line>1 Acacia Avenue</Line>
      <Line>London</Line>
    </Address>
  </Address>
</Person>


How could I change things so the the XML file produced is of this format:

<?xml version="1.0" encoding="utf-8"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Name>John Smith</Name>
  <Address>
    <Line>1 Acacia Avenue</Line>
    <Line>London</Line>
    <PostCode>WC1 1AA</PostCode>
  </Address>
</Person>

The changes required are
1) The <Address> element does not get repeated for the Address property of 
clsPerson and the Address arraylist in clsAddress.
2) The <PostCode> element appears below the <Line> elements

tia

Terry Holland


'=========================================================
Imports System.IO
Imports System.Xml.Serialization

Namespace Test

    <XmlRoot("Person", [Namespace]:="", IsNullable:=False)> _
    Public Class clsPerson

        Public Name As String = ""
        Public Address As clsAddress

        Public Sub New()
            Name = "John Smith"
            Address = New clsAddress
        End Sub

        Public Sub SerializeObject()
            Try
                ' Create a new XmlSerializer instance.
                Dim s As New 
Xml.Serialization.XmlSerializer(GetType(clsPerson))

                ' Writing the XML file to disk requires a TextWriter.
                Dim writer As New StreamWriter("C:\Test.xml")

                ' Serialize the object, and close the StreamWriter.
                s.Serialize(writer, Me)
                writer.Close()

            Catch x As System.InvalidOperationException
                Throw x
            End Try

        End Sub

    End Class

    Public Class clsAddress
        Private m_alLine As New ArrayList
        Public PostCode As String = ""

        Public Sub New()
            m_alLine.Add("1 Acacia Avenue")
            m_alLine.Add("London")
            PostCode = "WC1 1AA"
        End Sub

        <XmlArray("Address"), XmlArrayItem("Line", GetType(String))> _
        Public Property Address() As ArrayList
            Get
                Return m_alLine
            End Get
            Set(ByVal value As ArrayList)
                m_alLine = value
            End Set
        End Property

    End Class

End Namespace 




transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent