Groups | Blog | Home
all groups > dotnet xml > march 2006 >

dotnet xml : XmlArrayItem not allowed on Root Elements


Wilco
3/20/2006 10:49:20 AM
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?
dickster
3/21/2006 7:27:10 AM
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
dickster
3/21/2006 7:38:17 AM
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
AddThis Social Bookmark Button