Serialization in Windows Store Apps RRS feed

  • Question

  • I have been trying to serialize some of the properties in an app to save the state. I have tried both the XmlSerializer (which is what I used with Windows Phone, where it worked great) and the DataContractSerializer. The XmlSerializer did not seem to be completely available for Windows Store apps, so I have been looking for help on the DataContractSerializer (although I would have preferred the XmlSerializer). What I keep getting is the following exception:

    WinRT information: Error trying to serialize the value to be written to the application data store
    Additional information: Data of this type is not supported.

    I have added the DataContractAttribute to the class I am trying to serialize, as well as the class that is the type of one of it's properties. I have also added the DataMemberAttribute to their individual properties. The code I am using to do the serialization is:

    Dim dcs As New DataContractSerializer(GetType(MyClass()))
    Dim ms As New MemoryStream()
    dcs.WriteObject(ms, Me.Players)
    ms.Seek(0, SeekOrigin.Begin)
    ApplicationData.Current.LocalSettings.Values("players") = New StreamReader(ms).ReadToEnd()
    What am I doing wrong? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Tuesday, April 8, 2014 6:10 PM

All replies

  • Can you create a sample project and upload it to OneDrive or any other file sharing mechanism? That way it will be easier to understand your issue.

    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    Tuesday, April 8, 2014 11:02 PM
  • I'm not sure what you mean. My issue is simply that I cannot serialize my class; all I really need is a basic example of how to serialize an object in a Windows Store app (all the properties are either primitives or an instance of one of my other classes)

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Tuesday, April 8, 2014 11:21 PM
  • Here's a simple example I created, hopefully it will help.

    Public NotInheritable Class MainPage
        Inherits Page
        Protected Overrides Sub OnNavigatedTo(e As NavigationEventArgs)
            Dim serializer As XmlSerializer
            Dim stream As MemoryStream
            Dim aUser As User
            serializer = New XmlSerializer(GetType(User))
            stream = New MemoryStream()
            aUser = New User()
            aUser.firstName = "John"
            aUser.lastName = "Doe"
            serializer.Serialize(stream, aUser)
            stream.Seek(0, SeekOrigin.Begin)
            Dim reader As StreamReader
            reader = New StreamReader(stream)
            Dim str As String = reader.ReadToEnd()
            ApplicationData.Current.LocalSettings.Values("players") = str
        End Sub
    End Class
    Public NotInheritable Class User
        Public firstName As String
        Public lastName As String
    End Class

    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    Tuesday, April 8, 2014 11:59 PM
  • I tried that, but it did not help, I got the same error. Here is the exact code for my classes (well, the part that declares the properties), maybe that will help you figure out what is missing from my code:

    Public Class SorryPiece Public Property Position() As SorrySpace Public Property PlayerColor() As SorryColor Public Property MayEnterSafetyZone() As Boolean End Class Public Class SorrySpace Public Property BoardSide() As SorryColor Public Property Value() As Integer Public Property IsSafetyZone() As Boolean Public Property IsStart() As Boolean Public ReadOnly Property IsHome() As Boolean Get Return Me.Value = 64 End Get End Property End Class Public Enum SorryColor As SByte Red Blue Yellow Green End Enum

    I obviously don't need to serialize the IsHome property of SorrySpace because it is ReadOnly and calculated based on the other properties. Are there attributes I need to add to these classes to be able to serialize them? If so, which ones are they for the XmlSerializer or the DataContractSerializer? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Wednesday, April 9, 2014 1:07 AM