Answered by:
Serialize

Question
-
Hi, I'm trying to perfom Serializing/De- function. But I can't get how it's works in Win8.
I have a class Card needed to be serialized.
> Dim A As New XmlSerializer(GetType(Card))
Then I need to write this data to localstorage file
MSDN example shows:
' Create a FileStream to write with.
Dim writer As New FileStream(filename, FileMode.Create)
' Serialize the object, and close the TextWriter
serializer.Serialize(writer, i)
writer.Close()But there is no kind of FileStream in Win8. How to create file stream and write serialized data there?..
Aleksey
Thursday, December 5, 2013 10:26 PM
Answers
-
Here is how should looks reply
Dim Cards As New ObservableCollection(Of Card)
Cards.Add(New Card)
Cards(0).Name = "Hello!"
Dim sw = New StringWriter
Dim serializer = New XmlSerializer(GetType(ObservableCollection(Of Card)))
serializer.Serialize(sw, Cards)
Dim B As String = sw.ToString()
Dim Cards2 As New ObservableCollection(Of Card)
Dim sr As New StringReader(B)
Dim deserializer = New XmlSerializer(GetType(ObservableCollection(Of Card)))
Cards2 = deserializer.Deserialize(sr)Aleksey
- Marked as answer by Newfriend Saturday, December 7, 2013 1:38 AM
Saturday, December 7, 2013 1:38 AM
All replies
-
Yes there are file streams in WinRT. There is also support for XML serialization:
http://msdn.microsoft.com/en-us/library/bdxxw552(v=vs.110).aspx
Jeff Sanders (MSFT)
@jsandersrocks - Windows Store Developer Solutions @WSDevSol
Getting Started With Windows Azure Mobile Services development? Click here
Getting Started With Windows Phone or Store app development? Click here
My Team Blog: Windows Store & Phone Developer Solutions
My Blog: Http Client Protocol Issues (and other fun stuff I support)- Marked as answer by Jeff SandersMicrosoft employee, Moderator Friday, December 6, 2013 2:03 PM
- Unmarked as answer by Newfriend Friday, December 6, 2013 3:08 PM
Friday, December 6, 2013 2:03 PMModerator -
Windows.Storage.Streams has the streams you want.
Matt Small - Microsoft Escalation Engineer - Forum Moderator
If my reply answers your question, please mark this post as answered.
NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.Friday, December 6, 2013 2:08 PMModerator -
Please, check before post.
About your first link: http://social.msdn.microsoft.com/Search/en-US/windows/apps?query=serialize%20XML&Refinement=180&emptyWatermark=true&searchButtonTooltip=Search&ac=2
I'm opening the first XmlSerializer Class (System.Xml.Serialization) and coping the example:
Dim serializer As New XmlSerializer(GetType(Card))
Dim writer As New StreamWriter("myfile.txt") <--- Error: Value of type 'String' cannot be converted to 'System.IO.Stream'.
serializer.Serialize(writer, Cards)
writer.Close() <--- Error 'Close' is not a member of 'System.IO.StreamWriter'.Ok, fine, I'm opening your second link and coping example from there:
Dim serializer As New XmlSerializer(GetType(Card))
Dim writer As New FileStream("myfile.txt", FileMode.Create) <--- Error 1 Type 'FileStream' is not defined
serializer.Serialize(writer, Cards )
writer.Close()
Aleksey
- Edited by Newfriend Friday, December 6, 2013 3:21 PM
Friday, December 6, 2013 3:20 PM -
Is it possible to provide workable example? Thanks.
Aleksey
Friday, December 6, 2013 3:23 PM -
You are missing the using statements you need.
Full and complete sample is here: http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-5
Run this first and ensure you understand it and you will be able to apply this to your situation.
Jeff Sanders (MSFT)
@jsandersrocks - Windows Store Developer Solutions @WSDevSol
Getting Started With Windows Azure Mobile Services development? Click here
Getting Started With Windows Phone or Store app development? Click here
My Team Blog: Windows Store & Phone Developer Solutions
My Blog: Http Client Protocol Issues (and other fun stuff I support)Friday, December 6, 2013 3:37 PMModerator -
Here is how should looks reply
Dim Cards As New ObservableCollection(Of Card)
Cards.Add(New Card)
Cards(0).Name = "Hello!"
Dim sw = New StringWriter
Dim serializer = New XmlSerializer(GetType(ObservableCollection(Of Card)))
serializer.Serialize(sw, Cards)
Dim B As String = sw.ToString()
Dim Cards2 As New ObservableCollection(Of Card)
Dim sr As New StringReader(B)
Dim deserializer = New XmlSerializer(GetType(ObservableCollection(Of Card)))
Cards2 = deserializer.Deserialize(sr)Aleksey
- Marked as answer by Newfriend Saturday, December 7, 2013 1:38 AM
Saturday, December 7, 2013 1:38 AM