The second link is about XmlSerializer, but the first is not - it’s showing how you can use the XmlDocument interface. They’re very different.
XmlSerializer is really easy though, and that link explains it quite well. I don’t think you need a whole example project. What exactly do you want to load and save? How you’d integrate it into a project depends a lot on the kind of data you’re wanting to store.
Basically i need to load URLs, texts and images (url, to use it as textures on my objects).
I need to load some dynamic contents in a virtual building from an xml. I can understand what those script do, but i don’t know how to use them practically.
I mean take that script and put it on a GameObject and so on…
A noob guide on how to use it.
OK - so what do you want the data to look like when it’s been loaded - can you make a class that represents it? e.g. if your data was a bunch of cubes at various positions you could define something like this:
public struct Cube
{
public Vector3 position;
}
Then you can write an XML file (somehow) containing data like this:
It can contain as many Cube blocks as you like. Then you can use code like this to read in the list of cubes and, for example, create cube primitives based on the data:
void Start()
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Cube>));
StreamReader xmlText = new StreamReader("test.xml", true);
List<Cube> l = serializer.Deserialize(xmlText) as List<Cube>;
foreach (var cube in l)
{
GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
go.transform.position = cube.position;
}
}
I don’t know how much this helps you though because it’s just the same kind of thing as in the wiki page. To make something runnable, just create a C# script called XmlSerializationTest and paste the code below into it. Attach it to your camera (or some other GameObject). Then put your XML in “test.xml” in your project directory (the one with Assets, Library, etc as children). When you play your scene it should create the cubes.
using UnityEngine;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
public class XmlSerializationTest : MonoBehaviour
{
public struct Cube
{
public Vector3 position;
}
void Start()
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Cube>));
StreamReader xmlText = new StreamReader("test.xml", true);
List<Cube> l = serializer.Deserialize(xmlText) as List<Cube>;
foreach (var cube in l)
{
GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
go.transform.position = cube.position;
}
}
}