.NET Framework Developer Center > .NET Development Forums > ASMX Web Services and XML Serialization > Problem with XML Serialzation of Collection of String
Ask a questionAsk a question
 

AnswerProblem with XML Serialzation of Collection of String

  • Thursday, October 22, 2009 12:58 PMLuckyJack2001 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hello,
    I am using the XML Serializer to create an XML Schema. In one part of the XML, I have an element called 'parameters', which returns a collection of strings that should be called 'parameter'. The output is supposed to look like this:

    <parameters>
       <parameter>Value 1</parameter>
       <parameter>Value 2</parameter>
       <parameter>Value 3</parameter>
    </parameters>

    It's coming out like this:

    <parameters>
       <string>Value 1</string>
       <string>Value 2</string>
       <string>Value 3</string>
    </parameters>

    My class code looks like this:

    Public Class ParameterCollection
        Inherits System.Collections.ObjectModel.Collection(Of String)
    
        Public Sub New()
            MyBase.New()
        End Sub
        Public Overloads Sub Add(ByVal value As String)
            MyBase.Add(value)
        End Sub
        Default Public Overloads Property Item(ByVal index As Integer) As _
        <Xml.Serialization.XmlArrayItem(ElementName:="parameter", IsNullable:=True)> String
            Get
                Return MyBase.Item(index)
            End Get
            Set(ByVal value As String)
                If index <= Me.Count - 1 Then
                    Me.Item(index) = value
                Else
                    Throw New IndexOutOfRangeException()
                End If
            End Set
        End Property
        Public Overloads ReadOnly Property Count() As Integer
            Get
                Return MyBase.Count
            End Get
        End Property
        Public Overloads Sub Clear()
            MyBase.Clear()
        End Sub
        Public Overloads Sub RemoveAt(ByVal index As Integer)
            MyBase.RemoveAt(index)
        End Sub
    End Class
    
    I need the output to look exactly like my example at the top - I can't use an attribute, nor a subclass called 'parameter'. <br/>
    The output does not come out right (at least from what I've tried).  I tried using different combinations of xmlserializer directives, <br/>
    and I've tried an ArrayList in addition to the collection all with the same results. It seems as if the serializer is ignoring my directive<br/>
    to call the element a 'parameter' and just calling it 'string'.<br/>
    <br/>
    I am using VB .Net 2005.<br/>
    <br/>
    Can anyone help?<br/>
    <br/>
    Thanks!<br/>
    <br/>
    Paul.<br/>
    
    

Answers

  • Friday, October 30, 2009 12:48 AMDan Glick - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Is ParameterCollection the root of your document? If not, you can put the XmlArray and XmlArrayItem attributes on the property of type ParameterCollection; i.e:
    <XmlArrayItem(ElementName:="parameter")>
    Public Property Parameters As PropertyCollection
    

All Replies

  • Thursday, October 22, 2009 5:36 PMDaniel RothModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed Answer
    I think you want to put the XmlArrayItemAttribute on the class instead of on the Item property. 

    Daniel Roth
    ASMX, WCF
  • Thursday, October 22, 2009 6:38 PMLuckyJack2001 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi, Daniel,Oh, I tried that!  I tried every combination I could think of.  It always comes out as 'string'.
    -Paul
  • Thursday, October 22, 2009 6:49 PMLuckyJack2001 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hi again,
    To further clarify, I cannot put the xmlArrayItemAttribute on the class definition because it is not allowed:
    "Attribute 'XmlArrayAttribute' cannot be applied to 'ParameterCollection' because the attribute is not valid on this declaration type."

    These are the types of things I've tried:

        <Xml.Serialization.XmlArrayItem("parameter")> _
        <Xml.Serialization.XmlElement("parameter")> _
        Default Public Overloads Property Item(ByVal index As Integer) As _
        <Xml.Serialization.XmlArrayItem(ElementName:="parameter", IsNullable:=True)> String
            Get
                Return MyBase.Item(index)
            End Get
            Set(ByVal value As String)
                If index <= Me.Count - 1 Then
                    Me.Item(index) = value
                Else
                    Throw New IndexOutOfRangeException()
                End If
            End Set
        End Property
    
    -Paul
  • Thursday, October 29, 2009 3:34 PMLuckyJack2001 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello, can anyone help with this?  I am still stuck! 
    Thanks!

    Paul
  • Thursday, October 29, 2009 9:07 PMAmadeo Casas - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed Answer
    Have you tried implementing IXmlSerializable?
  • Friday, October 30, 2009 12:48 AMDan Glick - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Is ParameterCollection the root of your document? If not, you can put the XmlArray and XmlArrayItem attributes on the property of type ParameterCollection; i.e:
    <XmlArrayItem(ElementName:="parameter")>
    Public Property Parameters As PropertyCollection
    
  • Friday, October 30, 2009 12:22 PMLuckyJack2001 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    It worked!  It never occurred to me to put the directives on the higher-level class.  Once I did that it worked perfectly:

        <Xml.Serialization.XmlArrayItem(ElementName:="parameter")> _<br/>
        <Xml.Serialization.XmlArray("parameters")> _<br/>
        Public Property parameters() As ParameterCollection<br/>
            Get<br/>
                Return _parameters<br/>
            End Get<br/>
            Set(ByVal value As ParameterCollection)<br/>
                _parameters = value<br/>
            End Set<br/>
        End Property<br/>
    
    
    Thanks to all for the assistance!
    Paul