Serialization with Windows 8 Store Apps and VB

WEBINAR:On-Demand

Introduction

Any programmer will tell you that serialization is quite tricky in any circumstances. Add to the trickiness of serialization the fact that you have to serialize a Windows Store app, then you have double the trickiness! Today I will show you how to properly serialize Windows Store apps. Let's jump in.

Serialization

In case you do not know what serialization is, here is a small breakdown: Serialization is storing and retrieving data. This can be small pieces of information you need to save and retrieve, or large amounts of data. The choice of serialization ultimately depends on the amounts of data you need to save. Once the choice has been made, you need to realize what you're working with. This may sound strange, but you have to remember that Windows Store apps differ very much from desktop apps, and that you cannot implement the same logic into them as you'd have done with Desktop apps.

We cannot choose where we want to save small bits of info, it's done automatically. The only choice we have is to have the information always available or not. With today's example we will create a Windows Store app that stores a class object, and then reads the information stored back into our application. Let's create our project. Open up Visual Studio 2013 and choose to create a Windows Store app with Visual Basic. Design your main page as displayed in Figure 1.

Coding

The first issue to address is: What do we want to store? In our example we will have to store a student object and then later retrieve the info we have stored. We need to create a class called Student and give this class some properties. We will use the values of these properties our project to save and read. Let's add the Student class now by selecting Project, Add New Item, Class and give the name Student. Add the following code to it:

'Class To Be Serialized
Public Class Student
Private StuName As String 'Name
Private StuCourse As String 'Course
'StudentName Property
Public Property StudentName As String
Get
Return StuName
End Get
Set(value As String)
StuName = value
End Set
End Property
'StudentCourse Property
Public Property StudentCourse As String
Get
Return StuCourse
End Get
Set(value As String)
StuCourse = value
End Set
End Property
End Class

The Student class has only two properties: StudentName and StudentCourse. We will populate these properties with data, then save it to a file. Lastly, we will read this saved data back into the program. Let's move on to the MainPage's code. Add the next Import statements for all the necessary namespaces we need to include in our app to be able to write and read files:

Here in our WriteSudentInfo Function, we make use of the XMLSerializer class to save the needed info. That's not all. We needed to create the file ( if it didn't exist ) and then write the contents of our Student class through the use of the DataWriter class. With the button click, we simply populated the Student class' properties and called the WriteStudentInfo function to start the write process.

The file that is created and written to is named: data.xml and is stored at a location similar to C:\Users\UserName\AppData\Local\Packages\UniqueIdentifierNumber\LocalState, as displayed in Figure 2.

Inside the ReadStudentInfo Function, we read the info stored inside the data.xml file and deserialize it. We then simply display the file's contents inside our two listboxes.

Once run, and the Read button is clicked you will see a screen similar to Figure 3.

Figure 3 - Our working program

Conclusion

There are numerous ways to save info, XML Serialization just being one of the most useful of the lot. In my next article you will see what we can do without registry access in a Windows Store app. Until then, cheers!

Downloads

Comments

Developer

Posted by Alet
on 06/27/2015 05:54am

I learned a lot from this article. However, I noticed that the serialized .xml does not preserve the types of the attributes. (I tested it with some types of my own)
I'm looking for a solution that includes schema information (.xsd) but it looks like the stream classes used here won't allow that.