- Home /
How to edit XML assets at runtime?
I'm trying to find a way to dynamically edit XML data at runtime. At present I have been able to read XML from file by creating a TextAsset and using the XmlReader/StringReader classes to retrieve the data (note, I am deliberately not using an XML serialiser as I am using a custom structure to store the data). However, I need to be able to edit this information when the program is running and then convert this information back into XML format at the end of the program (therefore saving my changes so that they will load up the next time the program is run).
I am currently using the xmlWriter class (not the Deserialiser) to generate the amended XML from the data in my structure (it's a List<> of my own custom classes). I am able to create a new XML file reflecting the changes when I am in the editor but cannot find a way to get this working at runtime. I need to find a way to edit/overwrite the original saved XML data so it is dynamically edited each time it is used.
// Here's what I am reading the XML with:
TextAsset psml = (TextAsset)Resources.Load("psml",typeof(TextAsset));
StringReader xmlDoc = new StringReader (psml.text);
XmlReader myReader = XmlReader.Create (new StringReader(psml.text));
/*
Read the data in etc and make changes as the program is running...
*/
// And how I'm trying to create the amended file:
XmlWriter xmlWriter = XmlWriter.Create("Assets/Resources/psml",xmlWriterSettings);
Can anyone help/advise/send me a link please?
Answer by cashtotalszero · Jun 27, 2014 at 11:22 PM
I got this working on iOS and wrote up an answer here: http://answers.unity3d.com/questions/145686/moving-to-ios-xml-files.html if anyone else gets stuck here.