The following forum(s) have migrated to Microsoft Q&A (Preview): Developing Universal Windows apps!
Visit Microsoft Q&A (Preview) to post new questions.

Learn More

Error when reading from IsolatedStorage RRS feed

  • Question

  • When reading one of my files from IsolatedStorage, I receive the following error:

    "There is an error in XML document (3, 4)."

    Here is the code I have in my OnNavigatedFrom and OnNavigatedTo methods, which is where the XML is serialized and deserialized:

    Protected Overrides Sub OnNavigatedTo(e As System.Windows.Navigation.NavigationEventArgs) Using isf As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication() If isf.FileExists("Clocks.xml") Then Using stream As IsolatedStorageFileStream = isf.OpenFile("Clocks.xml", IO.FileMode.Open) Dim clocks As StorageClock() = CType(New XmlSerializer(GetType(StorageClock())).Deserialize(stream), StorageClock()) Dim currclock As Clock For i As Integer = 0 To 8 currclock = CType(Me.grdClocks.Children(i), Clock) currclock.FrontHour = clocks(i).FrontHour currclock.BackHour = clocks(i).BackHour currclock.IsCornerClock = clocks(i).IsCornerClock currclock.Quadrants = clocks(i).Quadrants Next End Using End If End Using MyBase.OnNavigatedTo(e) End Sub Protected Overrides Sub OnNavigatedFrom(e As System.Windows.Navigation.NavigationEventArgs) Using isf As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication() Dim xml As New XmlSerializer(GetType(StorageClock())) If isf.FileExists("Clocks.xml") Then isf.DeleteFile("Clocks.xml") Using stream As IsolatedStorageFileStream = isf.CreateFile("Clocks.xml") xml.Serialize(stream, (From ctrl As UIElement In Me.grdClocks.Children Where TypeOf (ctrl) Is Clock Select New StorageClock(CType(ctrl, Clock).FrontHour, CType(ctrl, Clock).BackHour, CType(ctrl, Clock).Quadrants, CType(ctrl, Clock).IsCornerClock)).ToArray()) End Using End Using MyBase.OnNavigatedFrom(e) End Sub

    And here is my definition for the StorageClock class:

    Public Class StorageClock
    	Public Property FrontHour() As Byte
    	Public Property BackHour() As Byte
    	Public Property IsCornerClock() As Boolean
    	Public Property Quadrants() As List(Of Byte)
    	Public Sub New(front As Byte, back As Byte, quadrants As List(Of Byte), corner As Boolean)
    		Me.FrontHour = front
    		Me.BackHour = back
    		Me.Quadrants = quadrants
    		Me.IsCornerClock = corner
    	End Sub
    End Class

    In my Clock class (which is a UserControl), I defined the Quadrants property using a TypeConverter so that I could enter the value in the XAML:

    <TypeConverter(GetType(ByteArrayTypeConverter))> Public Property Quadrants() As List(Of Byte)

    I was wondering if the TypeConverter could be somehow causing the error. I would like to be able to avoid the TypeConverter, but it was the only good way I could find to be able to enter the value in XAML. Any ideas? Thanks.

    Nathan Sokalski

    Thursday, December 20, 2012 9:47 PM


  • I found the problem. My StorageClock class could not be serialized because it did not have a parameterless constructor, so once I added that it fixed my problem.

    Nathan Sokalski

    Friday, December 21, 2012 3:32 AM