- Home /
XML File - Step 1?
Hey guys, this is my last hurdle to begin creating my game. I use Json saving and Java, so my XML knowledge is non-existant and all the tutorials seem to assume too much and begin with "just import this code > code"
Im using a plug-in tool, "Dialoguer" and i simply need an XML file to save a string variable, the command to save and everything is already present with the tool, its simply getting an XML file and interacting with it in unity which i dont know about.
So, What are the first steps to get an XML file into unity? (Does unity have the option or do i need visual studio?)
Thanks plenty
Answer by HarshadK · Jul 02, 2014 at 07:07 AM
You need to write code to read and write files in Unity.
This article about Saving Data#2 which uses XmlSerializer might be helpful for you.
You store your XML file from where you want to access it (preferably Assets folder) and then you access that XML file as you would access any other file from code.
The part from that tutorial where it loads and reads the XML file is as below:
To read from a file we just need to create an instance of XmlSerializer specialised to the root of our XML structure.
var xmlSerializer = new XmlSerializer();
Now we need to provide that serializer with the data from a stream - if you have a file you just open it:
var stream = File.Open("somefile.xml");
If we have a string then we might use this:
var stream = new MemoryStream (Encoding.UTF8.GetBytes (xml)); Then to create our entire structure we just do:
var library = xmlSerializer.Deserialize(stream);
And that's it - we've got the whole thing loaded. You should remember to close any File you've opened of course.
There is also an article on Unity Wiki about Saving and Loading Data: XmlSerializer
Before you jump right into something, I would suggest you to read those articles carefully. :-)
Thanks, although to obtain the initial X$$anonymous$$L file, how do i do that? Do i convert an empty C# or Java script file into being an X$$anonymous$$L file or do i need to create an X$$anonymous$$L file and import it into unity?
You create an X$$anonymous$$L file (create a file with extension .xml) and then import it into Unity.