By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,203 Members | 1,392 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,203 IT Pros & Developers. It's quick & easy.

XmlArrayItem not allowed on Root Elements

P: n/a
I'm trying to serialize a collection, which is the root element,
however the items in the collection contain derived classes, and thus
give an error without XmlArrayItem attributes. However I can't add
these to the field I'm copying from as its the root element, and so
they appear to get ignored. Now the obvious answer is to put them in a
wrapper and serialize the wrapper, which is okay, but I was wondering
why this restriction is in place is can you get around it?

Mar 20 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Any Use ??
************************************************** *********

<XmlRoot(ElementName:="WhatEverWontBeSeeingThis" )> _
Public Class root_team
Public _players() As Footballer
End Class

Public Class Footballer
Public nickname As String
Public team As String
End Class
---------------------------------------
Public Shared Sub Main()

Dim serializeAPP As XmlSerializer
Dim t As New root_team
Dim p1 As New Footballer
Dim p2 As New Footballer
Dim p3 As New Footballer
serializeAPP = New XmlSerializer(Type:=GetType(Footballer()),
root:=New XmlRootAttribute("FavPlayers"))

p1.nickname = "Gazza"
p1.team = "Newcastle"

p1.nickname = "Besty"
p1.team = "Manchester Utd"

p2.nickname = "Leeper"
p2.team = "Glentoran"

t._players = New Footballer(2) {p1, p2, p3}

serializeAPP.Serialize(Console.Out, t._players)
Console.Read()

End Sub

Mar 21 '06 #2

P: n/a
Any Use ??
************************************************** *********

<XmlRoot(ElementName:="WhatEverWontBeSeeingThis" )> _
Public Class root_team
Public players() As Footballer
End Class

Public Class Footballer
Public nickname As String
Public team As String
End Class

---------------------------------------
Public Shared Sub Main()

Dim xsr As XmlSerializer
Dim t As New root_team
Dim p1 As New Footballer
Dim p2 As New Footballer
Dim p3 As New Footballer

xsr = New
XmlSerializer(Type:=GetType(Footballer()),
root:=New XmlRootAttribute("FavPlayers"))

p1.nickname = "Gazza"
p1.team = "Newcastle"

p2.nickname = "Besty"
p2.team = "Manchester Utd"

p3.nickname = "Leeper"
p3.team = "Glentoran"

t.players = New Footballer(2) {p1, p2, p3}

xsr.Serialize(Console.Out, t._players)
Console.Read()

End Sub

Mar 21 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.