XML Serializer

Hallo, i am looking for a step by step tutorial on how to load values from a xml and use it in Unity.

I am lookin at this:

and this:

But i undestand that what i really need is an example project that use the xmlserializer to test it and study it.

Can you help? Thank you.

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:

<ArrayOfCube>
    <Cube>
        <position>
            <x>0.5</x>
            <y>0.2</y>
            <z>-4</z>
        </position>
    </Cube>
</ArrayOfCube>

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;
        }
    }
}

Wow, thank you very much. The information i need was exaclty:

:slight_smile:

How should it looks like in Javascript instead of C#? And how can i load the xml from a remote server?

I’ll test it right now. What i need is to do is making a virtual monitor on which i can load a texture from a given url from the xml.

I think something like:

</ArrayOfImages>
    <Images>
            <url1>myurl1<url1>
            <url2>myurl2<url2>
            <url3>myurl3<url3>
    </Images>
</ArrayOfImages>

I don’t know enough UnityScript to be able to translate the example.

To fetch from a remote server by HTTP, use the WWW class - the documentation has a good example.