Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
More Serialization Problems
Message
Information générale
Forum:
ASP.NET
Catégorie:
Visual Studio
Versions des environnements
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Desktop
Divers
Thread ID:
01593930
Message ID:
01593952
Vues:
37
The XmlSerializer in .NET can only bind to known static types (ie. compile time types). If anything dynamic or non-typed shows up in the serialization tree the XmlSerializer will choke unless you provide custom serialization for the the type (non-trivial).

Not sure what you're trying to serialize and whether it matters what the serialization format is, but you could use a JSON serializer. JSON.NET (and several other serializers) do much better with dynamic types and can figure out type configurations at runtime... So you can do things like serialize type object or collections of different types.

If you use the XmlSerializer everything has to be marked serializable, or inherit from MarshalByRefobject and everything has to have a hard type.

+++ Rick ---


>Hello Everybody,
>
>I have a collection that I an trying to serialize, but when I try to I get the following error message:
>
>The type SerializationExample02.User was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.
>
>Here is the code that I use to build my collection of classes:
>
>
>    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
>
>        Try
>
>            Dim oUserA As New User
>            Dim oUserB As New User
>            Dim oUserC As New User
>            Dim oUserD As New User
>
>            Dim oUsers06 As New Users06
>
>            With oUserA
>                .UserID = "A"
>                .UserName = "A Name"
>                .UserAccessLevel = 4
>                .UserPassword = "A's Secret Password"
>                oUsers06.Add(oUserA)
>            End With
>
>            With oUserB
>                .UserID = "B"
>                .UserName = "B Name"
>                .UserAccessLevel = 4
>                .UserPassword = "B's Secret Password"
>                oUsers06.Add(oUserB)
>            End With
>
>            With oUserC
>                .UserID = "C"
>                .UserName = "C Name"
>                .UserAccessLevel = 4
>                .UserPassword = "C's Secret Password"
>                oUsers06.Add(oUserC)
>            End With
>
>            With oUserD
>                .UserID = "D"
>                .UserName = "D Name"
>                .UserAccessLevel = 4
>                .UserPassword = "D's Secret Password"
>                oUsers06.Add(oUserD)
>            End With
>
>            'Dim oWriter As TextWriter = New StreamWriter("C:\Users\Gary Hagerty\Documents\Visual Studio 2012\Projects\SerializationExample02\SerializationExample02\Users.XML")
>            Dim oWriter As New StreamWriter("C:\Users\Gary Hagerty\Documents\Visual Studio 2012\Projects\SerializationExample02\SerializationExample02\Users06.XML")
>            Dim oXMLSerialize As New XmlSerializer(GetType(Users06))
>
>            oXMLSerialize.Serialize(oWriter, oUsers06)
>
>            oWriter.Close()
>
>        Catch oError As Exception
>
>            Dim sErrorString As String
>            sErrorString = "Message ---" & oError.Message
>            sErrorString = sErrorString & vbCrLf
>            sErrorString = sErrorString & "HelpLink ---" & oError.HelpLink
>            sErrorString = sErrorString & vbCrLf
>            sErrorString = sErrorString & "Source ---" & oError.Source
>            sErrorString = sErrorString & vbCrLf
>            sErrorString = sErrorString & "StackTrace ---" & oError.StackTrace
>            sErrorString = sErrorString & vbCrLf
>            sErrorString = sErrorString & "TargetSite ---" & oError.TargetSite.ToString
>            MessageBox.Show(sErrorString, "Error")
>
>            End
>
>        End Try
>
>    End Sub
>
> The code for my collection is as follows:
>
>Public Class Users06
>
>    Implements IList
>
>    Private moArray As ArrayList = New ArrayList()
>
>    Public Sub CopyTo(ByVal array As Array, _
>                      ByVal index As Integer) Implements ICollection.CopyTo
>
>        moArray.CopyTo(array, index)
>
>    End Sub
>
>    Public ReadOnly Property Count As Integer Implements ICollection.Count
>
>        Get
>            Count = moArray.Count
>        End Get
>
>    End Property
>
>    Public ReadOnly Property IsSynchronized As Boolean Implements ICollection.IsSynchronized
>
>        Get
>            Return False
>        End Get
>
>    End Property
>
>    Public ReadOnly Property SyncRoot As Object Implements ICollection.SyncRoot
>
>        Get
>            Return Me
>        End Get
>
>    End Property
>
>    Public Function GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
>
>        Return moArray.GetEnumerator()
>
>    End Function
>
>    Public Function Add(ByVal oUser As Object) As Integer Implements IList.Add
>
>        moArray.Add(oUser)
>        Return moArray.Count
>
>    End Function
>
>    Public Sub Clear() Implements IList.Clear
>
>        moArray.Clear()
>
>    End Sub
>
>    Public Function Contains(ByVal oUser As Object) As Boolean Implements IList.Contains
>
>        Contains = moArray.Contains(oUser)
>
>    End Function
>
>    Public Function IndexOf(ByVal oUser As Object) As Integer Implements IList.IndexOf
>
>        IndexOf = moArray.IndexOf(oUser)
>
>    End Function
>
>    Public Sub Insert(ByVal iIndex As Integer, _
>                      ByVal oUser As Object) Implements IList.Insert
>
>        moArray.Insert(iIndex, oUser)
>
>    End Sub
>
>    Public ReadOnly Property IsFixedSize As Boolean Implements IList.IsFixedSize
>
>        Get
>            Return False
>        End Get
>
>    End Property
>
>    Public ReadOnly Property IsReadOnly As Boolean Implements IList.IsReadOnly
>
>        Get
>            Return False
>        End Get
>
>    End Property
>
>    Default Public Property Item(ByVal iIndex As Integer) As Object Implements IList.Item
>
>        Get
>            Return CType(moArray.Item(iIndex), User)
>        End Get
>
>        Set(oUser As Object)
>            moArray.Item(iIndex) = oUser
>        End Set
>
>    End Property
>
>    Public Sub Remove(ByVal oUser As Object) Implements IList.Remove
>
>        moArray.Remove(oUser)
>
>    End Sub
>
>    Public Sub RemoveAt(ByVal iIndex As Integer) Implements IList.RemoveAt
>
>        moArray.RemoveAt(iIndex)
>
>    End Sub
>
>End Class
>
>The code for my class is as follows:
>
>Serializable() _
>Public Class User
>
>    Private miUserAccessLevel As Integer
>
>    Private msUserID As String
>    Private msUserName As String
>    Private msUserPassword As String
>
>    Public Property UserID() As String
>        Get
>            Return msUserID
>        End Get
>        Set(value As String)
>            msUserID = value
>        End Set
>    End Property
>
>    Public Property UserName() As String
>        Get
>            Return msUserName
>        End Get
>        Set(value As String)
>            msUserName = value
>        End Set
>    End Property
>
>    Public Property UserPassword() As String
>        Get
>            Return msUserPassword
>        End Get
>        Set(value As String)
>            msUserPassword = value
>        End Set
>    End Property
>
>    'Public Property UserLoggedIn() As Boolean
>    '    Get
>    '        Return mbUserLoggedIn
>    '    End Get
>    '    Set(value As Boolean)
>    '        mbUserLoggedIn = value
>    '    End Set
>    'End Property
>
>    Public Property UserAccessLevel() As Integer
>        Get
>            Return miUserAccessLevel
>        End Get
>        Set(value As Integer)
>            miUserAccessLevel = value
>        End Set
>    End Property
>
>End Class
>
>
>Please advise
>
>Thank you
>
>Gary
+++ Rick ---

West Wind Technologies
Maui, Hawaii

west-wind.com/
West Wind Message Board
Rick's Web Log
Markdown Monster
---
Making waves on the Web

Where do you want to surf today?
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform