T
Terry Holland
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
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