.NET Framework Developer Center >
.NET Development Forums
>
ASMX Web Services and XML Serialization
>
Problem with XML Serialzation of Collection of String
Problem with XML Serialzation of Collection of String
- 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
- 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
- Marked As Answer byLuckyJack2001 Friday, October 30, 2009 12:22 PM
All Replies
- I think you want to put the XmlArrayItemAttribute on the class instead of on the Item property.
Daniel Roth
ASMX, WCF- Proposed As Answer byDaniel RothModeratorThursday, October 22, 2009 5:37 PM
- Hi, Daniel,Oh, I tried that! I tried every combination I could think of. It always comes out as 'string'.
-Paul - 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:
-Paul<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 - Hello, can anyone help with this? I am still stuck!
Thanks!
Paul - Have you tried implementing IXmlSerializable?
- Proposed As Answer byAmadeo Casas - MSFTModeratorThursday, October 29, 2009 9:10 PM
- 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
- Marked As Answer byLuckyJack2001 Friday, October 30, 2009 12:22 PM
- It worked! It never occurred to me to put the directives on the higher-level class. Once I did that it worked perfectly:
Thanks to all for the assistance!<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/>
Paul


