jamc

jamc

  • NA
  • 4
  • 0

help!! element name changes case during xml serialization

Jan 18 2009 1:38 PM
I'm building an app that requires xml serialization and I've encountered a problem where the xmlserializer is changing the case of the root node during serialization.

I've created the following two classes; one to represent the object I'm working with and another to act as a collection.

<XmlRoot("user")> _
Public Class User
    Private propUserName As String

    <XmlAttribute("username")> _
    Public Property UserName() As String
        Get
            Return propUserName
        End Get
        Set(ByVal value As String)
            propUserName = value
        End Set
    End Property
End Class

and the second class to hold the user classes in a collection...

<XmlRoot("users")> _
Partial Public Class UserCollection
   
Inherits ArrayList

    <XmlElement(
"user")> _
   
Default Public Shadows Property Item(ByVal index As Integer) As User
       
Get
            Return MyBase
.Item(index)
       
End Get
       
Set(ByVal Item As User)
           
MyBase.Item(index) = Item
        End Set
    End Property
End Class


Serializing the user class on it's own produces an output I would expect;

<user username="value"/>

But serializing the user collection class produces a problem. For some reason the XmlSerializer converts the element names into pascal case (first letter is a capital) instead of camel case (i.e. myBase) - meaning that I get an output like this;

<users>
    <User username="value"/>
</users>


This causes a problem when I try to deserialize the xml string back into an object, as the XmlSerializer is expecting "user" not "User" (defined in both classes), and doesn't add the user class to the usercollection class like it should.

I've tried everything I can think of to get to the bottom of this one, but no luck. Can anyone throw some light on why this might be happening?

Answers (2)