- Home /
Saving new children/subchildren in XML
I've been following this page, and I've gotten stuck at the saving section of the code. I am trying to get every gameobject tagged tile, and save it's x, y, and tile type values.
I can get the variables from the objects. However, I cannot figure out how to save the XML document. I want to create a section for each object, and have subchildren data where it lists the x, y, and tile types.
<?xml version="1.0" encoding="UTF-8"?>
<ItemCollection>
<Items>
<Item index="0">
<XPos>0</XPos>
<YPos>0</YPos>
<TileType>0</TileType>
</Item>
<Item index="1">
<XPos>2</XPos>
<YPos>5</YPos>
<TileType>3</TileType>
</Item>
</Items>
</ItemCollection>
Above is an example of what XML document I want to generate.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.IO;
[XmlRoot("ItemCollection")]
public class ItemContainer {
[XmlArray("Items")]
[XmlArrayItem("Item")]
public List<Item> items = new List<Item>();
public static ItemContainer Load(string path)
{
TextAsset _xml = Resources.Load<TextAsset>(path);
XmlSerializer serializer = new XmlSerializer(typeof(ItemContainer));
StringReader reader = new StringReader(_xml.text);
ItemContainer items = serializer.Deserialize(reader) as ItemContainer;
reader.Close();
return items;
}
}
That is the code I am using for loading in data. How would I go about saving new data based upon variables, and resetting the XML document at each save (so it can reload all of the XML data)?
Anyone know what to do? I've been stuck on this for ages, and don't know how to do it.
Your answer
Follow this Question
Related Questions
DataContract serialization not compatible with Unity? 0 Answers
Serialization and XML 1 Answer
Storing list aka "save function" 3 Answers
how can i read this simple xml file ? 0 Answers
ArgumentException: Path is empty while saving data in an XML 0 Answers