- Home /
Ho do I read a text file on Android (and iPhone)?
For our game, we save level data in XML. The files are serialised classes, and the system works fine on PC using C# classes such as XMLSerialiser
and TextReader
.
Now on android, I can't figure out how to get it to work on Android. The first problem seems that the files are simply not found (I have located them in Assets/Levels/
, and just for interest sake, I also tested Assets/Resources/Levels/
).
Any ideas?
I know about TextAsset, and that I can get a string through it via Resources.Load, which I can then parse (!!!). I am of course on the lookout for a method that works with C#'s parsing tools.
Answer by Herman-Tulleken · Mar 10, 2011 at 11:12 AM
I ended up using the TextAsset, and then use a chain of readers etc., to get the serialized object. Here is the code for anyone interested:
//xml is a string containing XML (potentially from a TextAsset). public static T DeserializeString<T>(string xml) { XmlSerializer serializer = new XmlSerializer(typeof(T)); StringReader stringReader = new StringReader(xml); XmlTextReader xmlReader = new XmlTextReader(stringReader); T obj = (T) serializer.Deserialize(xmlReader);
xmlReader.Close();
stringReader.Close();
return obj;
}
Your answer
Follow this Question
Related Questions
xmlreader android path problem 0 Answers
Android Persistant Data 1 Answer
Xml file not going in APK ... 1 Answer
How to modify or save xml on Android 0 Answers
StreamReader can't read Android internal storage path 0 Answers