2
Answers

help!! element name changes case during xml serialization

jamc

jamc

16y
5.6k
1
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)
0
aadi kumary
NA 12 10.9k 10y


Hi,

is their any way to display AlternationIndex ?


Below is my code this displaying 0 only ...




<ListBox x:Name="listBox" AlternationCount="3">

 

        <ListBox.ItemTemplate >

                <DataTemplate >

                    <Canvas Margin="0,20,0,0">

                        <StackPanel Orientation="Vertical">

<TextBlock VerticalAlignment="Top" Text="{Binding Name }" FontSize="16" FontWeight="Bold" Margin="30,20,0,0"  Foreground="Black" />

 <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},

                                      Path=(ItemsControl.AlternationIndex)}" />

 

                        </StackPanel>

                    </Canvas>

                </DataTemplate>

            </ListBox.ItemTemplate>

        </ListBox>

0
Khan Abrar Ahmed
NA 5.8k 200k 10y
Hi, check the image path
<Image Source="Images\Tiles\Folder-n-design(right).png" Name="imgLoadRight" Margin="0,10,0,0" Loaded="imgLoad_Loaded_1" Visibility="Collapsed" />
<Image Source="Images\Tiles\Folder-n-design(center).png" Name="imgLoadCenter" Margin="0,100,0,0" Loaded="imgLoad_Loaded_1" Visibility="Collapsed" />