- Home /
XML Serialization in Windows phone 8
Hi, I'm using Xml Serialization in my game for saving and loading data.But Windows phone 8 doesn't support it.This is how I've serialized in my Android,IOS and its working fine
public static void SaveXmlFile(GameDataBase d)
{
XmlSerializer serialize = new XmlSerializer (typeof(GameDataBase));
TextWriter writer = new StringWriter() ;
serialize.Serialize (writer,d);
File.WriteAllText (FilePath+"/"+FileName,writer.ToString ());
}
public static GameDataBase LoadXmlFile()
{
string str = File.ReadAllText (FilePath+"/"+FileName);
XmlSerializer deSerialize = new XmlSerializer (typeof(GameDataBase));
TextReader reader = new StringReader(str) ;
GameDataBase gData = (GameDataBase)deSerialize.Deserialize (reader);
return gData;
}
But when I port it in Windows phone I get the error
Error building Player: Exception: Error: method
System.Void > System.IO.File::WriteAllText(System.String,System.String)
doesn't exist in target framework. It is referenced from Assembly-CSharp.dll at System.Void XMLSerialize::SaveXmlFile(AddressDirectory). Error: methodSystem.String > System.IO.File::ReadAllText(System.String)
doesn't exist in target framework. It is referenced from Assembly-CSharp.dll at AddressDirectory XMLSerialize::LoadXmlFile(). Error: methodSystem.Void > System.IO.File::WriteAllText(System.String,System.String)
doesn't exist in target framework. It is referenced from Assembly-CSharp.dll at System.Void XMLSerialization::SaveXmlFile(GameDataBase). Error: methodSystem.String > System.IO.File::ReadAllText(System.String)
doesn't exist in target framework. It is referenced from Assembly-CSharp.dll at GameDataBase XMLSerialization::LoadXmlFile(). Error: methodSystem.String > System.Text.Encoding::GetString(System.Byte[])
doesn't exist in target framework. It is referenced from Assembly-CSharp.dll at System.String _GameSaveLoad::UTF8ByteArrayToString(System.Byte[]).
Answer by Landern · Apr 23, 2015 at 12:33 PM
Windows Phone doesn't allow the usage of System.IO as described in the header of this page. They have their own scheme for Isolated Storage calls for type of data manipulation and storage.
If this is a multiple platform project, you will need to use some compiler directives(UNITY_METRO, UNITY_WP_8_1, UNITY_WINRT, UNITY_WINRT_8_1) to change the storage medium on windows phone.
Answer by dsrgdeg · Apr 24, 2015 at 02:28 PM
Hi Landern,
I was unable to post this as a comment. My xml Serialization code is working perfectly fine in the editor but when I'm trying to build it on windows phone it is giving me an error
IsolatedStorageException: No ApplicationIdentity available for AppDomain. System.IO.IsolatedStorage.IsolatedStorage.InitStore (IsolatedStorageScope scope, System.Type appEvidenceType) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.IO.IsolatedStorage/IsolatedStorage.cs:156) (wrapper remoting-invoke-with-check) System.IO.IsolatedStorage.IsolatedStorage:InitStore (System.IO.IsolatedStorage.IsolatedStorageScope,System.Type) System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication () (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.IO.IsolatedStorage/IsolatedStorageFile.cs:247) XMLSerialize.Start () (at Assets/Demo/XMLSerialize.cs:37)
Here's a look at my code
using UnityEngine;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.IO;
using UnityEngine.UI;
using System.IO.IsolatedStorage;
//using Windows.Storage;
#if NETFX_CORE
using XmlReader = WinRTLegacy.Xml.XmlReader;
#else
using XmlReader = System.Xml.XmlReader;
#endif
public class XMLSerialize : MonoBehaviour {
TextAsset textFile;
static AddressDirectory obj;
public Text showtext;
// Use this for initialization
void Start () {
//
showtext = GetComponent<Text>();
//showtext.text = "house:";
obj = new AddressDirectory ();
obj.addressList = new List<Address> ();
obj.addressList.Add (new Address());
obj.addressList [0].HouseNo = 50;
// SaveXmlFile (obj);
// AddressDirectory obj2 = LoadXmlFile ();
// print (obj2.addressList [0].HouseNo);
//
// var storage = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
// IsolatedStorageFile.GetStore( IsolatedStorageScope.User
// | IsolatedStorageScope.Assembly, null, null );
string[] fileNames = isoStore.GetFileNames( "temp.xml");
print (fileNames.Length);
foreach ( string file in fileNames )
{
if ( file =="temp.xml" )
{
Debug.Log("The file already exists!");
}
}
IsolatedStorageFileStream oStream =
new IsolatedStorageFileStream( "temp.xml",
FileMode.Create, isoStore );
StreamWriter writer = new StreamWriter( oStream );
XmlSerializer serialize = new XmlSerializer (typeof(AddressDirectory));
serialize.Serialize (writer,obj);
writer.Close();
AddressDirectory obj2 = LoadXmlFile ();
print (obj2.addressList [0].HouseNo);
// showtext.text = "house:"+obj2.addressList [0].HouseNo;
}
public static void SaveXmlFile(AddressDirectory d)
{
IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream stream = null;
stream = new IsolatedStorageFileStream("temp.xml",
FileMode.OpenOrCreate,
FileAccess.ReadWrite,
storage);
XmlSerializer serializer = new XmlSerializer(typeof (AddressDirectory));
serializer.Serialize(stream, obj);
// XmlSerializer serialize = new XmlSerializer (typeof(AddressDirectory));
// TextWriter writer = new StringWriter() ;
// serialize.Serialize (writer,d);
// File.WriteAllText (Application.dataPath+"/temp.xml",writer.ToString ());
// print (writer+ " " );
}
public static AddressDirectory LoadXmlFile()
{
//
// IsolatedStorageFile isoStore =
// IsolatedStorageFile.GetStore( IsolatedStorageScope.User
// | IsolatedStorageScope.Assembly, null, null );
IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream oStream =
new IsolatedStorageFileStream( "temp.xml",
FileMode.Open, isoStore );
XmlSerializer serializer = new XmlSerializer(typeof (AddressDirectory));
//serializer.Serialize(stream, obj);
AddressDirectory gData = (AddressDirectory)serializer.Deserialize (oStream);
// string str = File.ReadAllText (Application.dataPath+"/temp.xml");
// XmlSerializer deSerialize = new XmlSerializer (typeof(AddressDirectory));
// TextReader reader = new StringReader(str) ;
// AddressDirectory gData = (AddressDirectory)deSerialize.Deserialize (reader);
//print (str);
return gData;
}
}
public class AddressDirectory
{
[XmlElement("Address")]
public List<Address> addressList = new List<Address>();
}
public class Address
{
public int HouseNo ;
public string StreetName;
public string City ;
public Address()
{
HouseNo = 90;
}
}