- Home /
How to serialize and save for Win8?
Because .Net 4.5 blacklisted a bunch of old assemblies, all the ways I know for serializinf/deserializing and saveing/loading files don't work when building from Unity to Win8. I've been trying to figure out something new, but progress is slow because Mono runs on an earlier version of .NET so it allows things that I can't actually use. I would really appreciate some advice on fairly simple ways to serialize classes and save them out when making games for Win8.
Hi, what are you trying to serialize/deserialize to/from? X$$anonymous$$L?
Anything at this point. I think I'd prefer JSON, but X$$anonymous$$L is fine.
Answer by Mithosbluefish · Aug 11, 2013 at 10:16 PM
Here's a few methods that serialize and deserialize to and from XML using XmlSerializer, According to [msdn][1] it is supported in .Net 4.5, this will allow you to input the object you want to serialize and then serialize it into Xml and then input a XMLNode.OuterXML and a XMLSerializer (containing the node type) deserialize it.
public XmlDocument SerializeObject(object pObject)
{
XmlSerializer xmlSer = new XmlSerializer(pObject.GetType());
TextWriter textWriter = new StringWriter();
xmlSer.Serialize(textWriter, pObject);
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(textWriter.ToString());
return xdoc;
}
public object DeserializeObject(string pXmlizedString, XmlSerializer xs)
{
MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
return xs.Deserialize(memoryStream);
}
byte[] StringToUTF8ByteArray(string pXmlString)
{
UTF8Encoding encoding = new UTF8Encoding();
byte[] byteArray = encoding.GetBytes(pXmlString);
return byteArray;
}
Hope this helped :) [1]: http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx
Thanks so much, I'll try this out and let you know how it goes.
Just tested a script using it in Windows 8 and it ran exactly as it should be :)
Do you mean it worked when running Unity on Win8 or that it works when you build to Win8? When I try to build that code as a Windows Store App it gets mad at XmlSerializer :(
I used a build, as the only win8 pc I have handy doesn't have unity installed. Worked fine for me, if you want i can help you figure out whats wrong.
Answer by Mithosbluefish · Aug 12, 2013 at 12:35 AM
Here's also something that might help you with JSON serialization. :) http://msdn.microsoft.com/en-us/library/bb412179.aspx
Answer by lancer · Aug 12, 2013 at 01:57 AM
There is a good Serializer I got from http://whydoit.com/. Very easy to use and well documented.
Your answer
