Re: Serialization Question
- From: "Terry Holland" <nospam@xxxxxxxxxxxx>
- Date: Mon, 16 Feb 2009 20:16:54 -0000
If it were me, I would probably have an object like so:
AddressLine1
AddressLine2
City
StateProvince, only when applicable, so this does not fit all countries
PostalCode
I can then set up the output according to country, so your address would
be
AddressLine1
AddressLine2 (optional)
City
PostalCode
I'll give you a bit more of the background. Our company need to transmit
files to a partner organisation and they are expecting an XML file of the
format I described. In particular their scheme definition for the address
is defined below
<xs:element name="Address" type="UKPostalAddressStructure"/>
.....
<xs:complexType name="UKPostalAddressStructure">
<xs:sequence>
<xs:element name="Line" type="xs:string" minOccurs="2" maxOccurs="6"/>
<xs:element name="PostCode" type="xs:string"/>
</xs:sequence>
</xs:complexType>
Question 2: Why is format of the XML important?
The only reason I can see is you are trying to serialize this information
and present it, via some sort of service boundary, as XML, not as a
serialized object. If so, I would take the XML through a transformation to
get the format you desire rather than attempt to have the object serialize
differently.
You are correct that the format is based on an external requirement. Im not
too hot on XML so when you say "...take the XML through a transformation...
" could you expand a bit on this. I have overcome my problem in the
immediate term by manually creating an XMLDocument in the desired format and
transmitting this document (I needed to get something out urgently). I
would however be interested in exploring transformations for when I refactor
NOTE:
The reason why your XML is not like this:
<?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>
is two fold:
1. The inner "Address" nodes represent another object. In this case, it isIf I had Address as Array(of String) then all parts of address would have
an ArrayList, but it could have been Array(of String) or other type of
list object, while the PostalCode is a separate property.
serialized as <Line> elements, including the Postcode
2. The ordering, I believe, comes from the order in the object, so you canThe order in the class doesnt seem to make any difference ie Postcode
end up with the Address lines before the postal code by changing order of
the list and the string. If this does not work, there is a hierarchy that
simple types are rendered prior to complex types, but I do not think this
is the case.
property appears after Address property in class but before in the
serialized file
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
Blog:
http://feeds.feedburner.com/GregoryBeamer
*************************************************
| Think outside the box! |
*************************************************
"Terry Holland" <nospam@xxxxxxxxxxxx> wrote in message
news:ePWLo7BkJHA.1172@xxxxxxxxxxxxxxxxxxxxxxx
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
.
- Follow-Ups:
- Re: Serialization Question
- From: Cowboy \(Gregory A. Beamer\)
- Re: Serialization Question
- References:
- Serialization Question
- From: Terry Holland
- Re: Serialization Question
- From: Cowboy \(Gregory A. Beamer\)
- Serialization Question
- Prev by Date: Re: Is linq the final straw for VB?
- Next by Date: Re: Is linq the final straw for VB?
- Previous by thread: Re: Serialization Question
- Next by thread: Re: Serialization Question
- Index(es):
Relevant Pages
|