- Home /
Coding help: How to use xml serialization
Hello, I've seen that xml serialization is a very common way to save files and I want to use this because: -System.Runtime.Serialzation.Binary.Formatters doesn't allow to serialize on Windows Phone -PlayerPrefs is very good but can't be used for lists.
So let's get to the point: I want to save this list with xml: public List<string> Collected = new List<string>();
Could anyone help me do this or at least give me a simple example on how to save/load my list. I've looked on unity wiki, google it, search everywhere but still I can't understand the logic it uses as I am new to programming
Thank You!
Answer by cjdev · Aug 17, 2015 at 09:10 PM
There are two things you'll need here, the creation of the xml document from the list and then the serialization of the document. I'd recommend using the XDocument and XElement classes and putting the xml file in your Resources folder.
using System.Collections.Generic;
using System.Xml.Linq;
//How to create and populate a new xml document from a list
XDocument doc = new XDocument();
doc.Add(new XElement("Items"));
foreach (string item in Collected)
{
doc.Element("Items").Add(new XElement("Item", item));
}
This will give you an xml document with the following format:
<?xml version="1.0" encoding="utf-8"?>
<Items>
<Item>CollectedValue1</Item>
<Item>CollectedValue2</Item>
<Item>CollectedValue3</Item>
<Item>CollectedValue4</Item>
...
</Items>
And finally, once you have your xml structured you can read and write it to a file as you like:
//This is how you load a xml document from your Resources folder
TextAsset xmlText = Resources.Load("CollectedXML") as TextAsset;
XDocument myData = XDocument.Parse(xmlText.text);
//And this is how you write it
myData.Save(Application.dataPath + "/Resources/CollectedXML.xml");
Thank you for your quick reply! But after that how I can apply the xml document's content to the list "collected"?
You can do this:
List<XElement> Collected = doc.Descendants("Item").ToList();
Also check out LINQ to X$$anonymous$$L for more sorting and searching functions you can do with your XElement objects.
Gives me this error:
Assets/Scripts/GameController.cs(69,72): error CS1061: Type System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>' does not contain a definition for
ToList' and no extension method ToList' of type
System.Collections.Generic.IEnumerable' could be found (are you missing a using directive or an assembly reference?)
Probably there isn't a definition for "ToList"
Sorry, you'll need to add using System.Linq;
for that.
Also I just saw you'd probably want the Collected list in a string type so you could use a bit of LINQ to do that like this:
doc.Descendants("Item").Select(x => x.Value).ToList();
Either I am messing something or it's just to difficult I have given all my code below... I don't know what I did wrong :/
using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Xml.Linq;
using System.Linq;
public class GameController : $$anonymous$$onoBehaviour {
public static GameController Controller;
public int Lives;
public int Wumpas;
public int Health;
// Use this for initialization
void Awake () {
if (Controller == null) {
DontDestroyOnLoad (gameObject);
Controller = this;
}
else if (Controller != null) {
Destroy(gameObject);
}
}
// Update is called once per frame
void Update () {
}
public void Save() {
PlayerPrefs.SetInt ("Wumpas", Wumpas);
PlayerPrefs.SetInt ("Lives", Lives);
PlayerPrefs.SetInt ("Health", Health);
$$anonymous$$akeDocument ();
}
public void Load() {
Wumpas = PlayerPrefs.GetInt ("Wumpas");
Lives = PlayerPrefs.GetInt ("Lives");
Health = PlayerPrefs.GetInt ("Health");
Load2();
}
//public List<string> Uncollected = new List<string>();
public List<string> Collected = new List<string>();
public void Earned(string name) {
//Uncollected.Remove(name);
Collected.Add(name);
}
public void $$anonymous$$akeDocument() {
//How to create and populate a new xml document from a list
XDocument doc = new XDocument ();
doc.Add (new XElement ("Items"));
foreach (string item in Collected) {
doc.Element ("Items").Add (new XElement ("Item", item));
}
//doc.Save(Application.dataPath + "/CollectedX$$anonymous$$L.xml");
doc.Save(Application.dataPath + "/Resources/CollectedX$$anonymous$$L.xml");
Debug.Log ("File saved on: " + Application.dataPath + "/Resources/CollectedX$$anonymous$$L.xml");
}
public void Load2() {
//TextAsset xmlText = Resources.Load("CollectedX$$anonymous$$L") as TextAsset;
TextAsset xmlText = Resources.Load ("CollectedX$$anonymous$$L") as TextAsset;
XDocument myData = XDocument.Parse(xmlText.text);
}
public void Save2() {
XDocument myData = new XDocument();
myData.Save(Application.dataPath + "/Resources/CollectedX$$anonymous$$L.xml");
}
}
Your answer
Follow this Question
Related Questions
Save and load serialization problem 0 Answers
Can't get serialization Load/Save to work!C# 3 Answers
My save function saves the wrong values!! 1 Answer
Unity Saved Serialized Levels 0 Answers