I finally decided to stop being scared of xml and just learn it already and stop avoiding it. And now I’m so confused. I’ve been through dozens of google/forums/youtube searches and I can not understand this XML serialization. Its all alien language to me.
First it took me a bit to realize that there’s a few ways to go about reading and writing in xml files. There’s that XmlDocument thing, and theres the serialization, and probably something else too. If I understood it correctly, those two things are different ways, and serializing is better. I sort of understood the document way, but serialization is confusing me even more.
Now I’ve been to the unify community tutorial about it and I don’t get it. How exactly are you reading or writing anything? I just don’t see it happening in the code. And why do I need more than one script? I can’t find a simple example at all on how to do this. Or I’m just too stupid for this.
I wanted to learn by making a simple checklist program, and saving/loading the data from an xml file.
Here’s the short version:
<Checklists>
<Checklist_1>
<Day_1>
<Thing_1>example 1</Thing_1>
<Thing_2>example 2</Thing_2>
</Day_1>
<Day_2>
<Thing_1>example 1</Thing_1>
<Thing_2>example 2</Thing_2>
</Day_2>
</Checklist_1>
<Checklist_2>
<Day_1>
<Thing_1>example 1</Thing_1>
<Thing_2>example 2</Thing_2>
</Day_1>
<Day_2>
<Thing_1>example 1</Thing_1>
<Thing_2>example 2</Thing_2>
</Day_2>
</Checklist_2>
</Checklists>
Now what to do in actual code? It got confusing real fast because this turns out to be an array within an array within an array.
This is as far as I got:
[XmlRoot("Checklists")]
public class Checklists
{
[XmlArray("Checklist_1")]
[XmlArrayItem("Day_1")]
public List<Day_1> Checklist_1 = new List<Day_1>();
}
public class Day_1
{
public string Thing_1;
public string Thing_2;
}
Yeah I know… its sad. Now if this is correct at all, I don’t understand the reading and writing part in the tutorial. I don’t see any variables like health being assigned or read there. Is it because of the [XmlRoot("Checklists")]
part? The vars and the xml file are being connected? And then the read write is just syncing everything from and to? What if you just wanted to sync one var? And how is then the public class Day_1 being connected with the file? Because its being declared in Checklists class? Or am I completely off on this?
Thank you.