Trouble serializing arrays - they wrap all their children..

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,081
Programming Experience
10+
I'm trying to generate some xml like this:

VB.NET:
<root>
  <address>
    <child1/>
    <child2/>
  </address>
  <address>
    <child1/>
    <child2/>
  </address>
  <zzz>
    <child1/>
  </zzz>
</root>

Of concern is the address.. It is to be such that the first block of address is the current address. the previous address comes second

My code looks something like:

VB.NET:
[System.Xml.Serialization.XmlArray("address")]
Public addresses(0 to 1) as New AddressXMLBlockRepresentation

[System.Xml.Serialization.XmlIgnore()]
Public Property CurrentAddress
  Get
    Return addresses(0)
  End Get
  Set
    addresses(0) = value
  End Set
End Property

[System.Xml.Serialization.XmlIgnore()]
Public Property PreviousAddress
  Get
    Return addresses(1)
  End Get
  Set
    addresses(1) = value
  End Set
End Property

The xml formed, annoyingly, is:

VB.NET:
<root>
  [B]<AddressXMLBlockRepresentation>[/B]
    <address>
      <child1/>
      <child2/>
    </address>
    <address>
      <child1/>
      <child2/>
    </address>
 [B] </AddressXMLBlockRepresentation>[/B]
  <zzz>
    <child1/>
  </zzz>
</root>


Anyone know how to get the array to serialise out as a sequence of elements <address> without the enclosing type?

I cant just ignore the array and label each Property with
[System.Xml.Serialization.XmlElement("address")]
Because it errors on reflecting - "address" appears twice

Also, of further nuisance, the serializer will only serialize public fields, which means my array has to be public, which means it can be seen by other coders. The idea was to develop a library that was more foolproof in this regard. Any ideas?
 
Do I have any control over the order of serialization? As presented before, experian impose rules according to the orde rof elements in a document.. Now this may not bite right now, because the array elements themselves are indeed serializing in order.. The issue I have is that it seems member variables are serialized first, then properties.

I might be able to do this by making the array member private, but have a property that returns the array, mixed in with the rest of the properties - This works.. But the property must be a get/set one. By making it get only, it wont show in the generated XML (Presumably because this xml serializer doesnt know it will be used in One Way mode only..



Additionally, as mentioned, I'd really like to hide the array from a coder in intellisense, but have it shown in the xml. I've so far called the property DO_NOT_USE and set the following attributes:
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]

I'm hoping this will be enough; it isnt within my assembly but it might hide it when compiled as a dll


Right now because the array has to be public for serialization, it shows in intellisense.. How do i hide it from IS, but have it remain visible to XML Ser?
 
Last edited:
Fields should be private, accessed from outside the class through properties. (Guidelines: Field Design). If I understand your array issue, you want to hide the array and have separate property accessors for the items in the array?
 
Fields should be private, accessed from outside the class through properties. (Guidelines: Field Design). If I understand your array issue, you want to hide the array and have separate property accessors for the items in the array?

Correct, but I want the elements of the array to all serialize to ADDR tags in XML.. I cant make both properties serialize to the same name, so i must make the array public..
 
Implementing IXmlSerializable interface could also be an option, you would then write code for WriteXml/ReadXml methods to serialize the class content to Xml elements and attributes any way you want.
 
This might help you

http://www.codeproject.com/useritems/CustomXmlSerializer.asp

i used his codes and generated the xml formed like this
VB.NET:
- <PrintColNames size="2" className="System.Array" type="PrintEngine.PrintColNames">
- <System.Array.Item point="0">
- <PrintColNames>
  <ValueOfField>FieldName0</ValueOfField> 
  <XPostition>0</XPostition> 
  <YPostition>0</YPostition> 
  </PrintColNames>
  </System.Array.Item>
- <System.Array.Item point="1">
- <PrintColNames>
  <ValueOfField>FieldName1</ValueOfField> 
  <XPostition>300</XPostition> 
  <YPostition>300</YPostition> 
  </PrintColNames>
  </System.Array.Item>
  </PrintColNames>


And this is my class that i serialized
VB.NET:
Public Class PrintColNames

    Private fieldValue As String
    Private xPos, yPos As Integer

    Public Sub New()

    End Sub



    Public Property ValueOfField() As String

        Get
            Return Me.fieldValue
        End Get

        Set(ByVal value As String)
            Me.fieldValue = value
        End Set

    End Property

    Public Property XPostition() As Integer
        Get
            Return Me.xPos
        End Get
        Set(ByVal value As Integer)
            Me.xPos = value
        End Set
    End Property

    Public Property YPostition() As Integer
        Get
            Return Me.yPos
        End Get
        Set(ByVal value As Integer)
            Me.yPos = value
        End Set
    End Property

End Class

Hopes it helps :)
 
Back
Top