XML Deserialize Dialog and Objectives

I’ve been trying a while now to get an XML document full of DialogItems and Objectives to be deserialized and for it to populate text objects at runtime.

Specifically dialog lines to change in time with audio files, and then objectives per level.
I’ve tried following several tutorials but can never quite get it to work. Even this one, which I thought was closer to what I had in mind.

Can anyone point me in the right direction? I also have a Dialog class and as I understand it I’d need a script to actually deserialize it, but I also expect I’ll need a separate script to access the deserialized data and populate said text objects.

Thanks in advance

<?xml version="1.0" encoding="utf-8"?>

<All>
    <DialogList>
        <DialogItem>
            <LL-1>
                <num>1</num>
                <level>Lobby</level>
                <content>
                    This is a line of dialog 1.
                </content>
            </LL-1>
        </DialogItem>
        <DialogItem>
            <LL-2>
                <num>2</num>
                <level>Lobby</level>
                <content>
                    This is a line of dialog 2.
                </content>
            </LL-2>
        </DialogItem>
        <DialogItem>
            <LL-1-alt>
                <num>0</num>
                <level>Lobby</level>
                <content>
                    This is an alternate line of dialog 1.
                </content>
            </LL-1-alt>
        </DialogItem>
        <DialogItem>
            <L1-1>
                <num>3</num>
                <level>1</level>
                <content>
                    This is a line of dialog 3.
                </content>
            </L1-1>
        </DialogItem>
        <DialogItem>
            <L1-2>
                <num>4</num>
                <level>1</level>
                <content>
                    This is a line of dialog 4.
                </content>
            </L1-2>
        </DialogItem>
        <DialogItem>
            <L1-3>
                <num>5</num>
                <level>1</level>
                <content>
                    This is a line of dialog 5.
                </content>
            </L1-3>
        </DialogItem>
        <DialogItem>
            <L1-4>
                <num>6</num>
                <level>1</level>
                <content>
                    This is a line of dialog 6.
                </content>
            </L1-4>
        </DialogItem>
    </DialogList>
   
    <ObjectivesList>
        <Objective>
            <O1-1>
                This is an objective for level 1.
            </O1-1>
        </Objective>
    </ObjectivesList>
</All>
using System.Xml;
using System.Xml.Serialization;
using System.IO;

[XmlRoot("All")]
public class Dialog
{
    [XmlAttribute("num")]
    public int num { get; set; }
    [XmlAttribute("level")]
    public string level { get; set; }
    public string content { get; set; }
}

If this is to go in Unity, I highly recommend just using ScriptableObjects to hold all the contents you need. There are plenty of tutorials out there for dialog systems based on this approach, and just generally tutorials on using ScriptableObjects. They’re VERY powerful in the Unity editor.

If you insist on parsing your own serialized data, XML is the last thing I would consider. JSON is far cleaner and has a huge advantage over XML: JSON actually works.

Remember, if you have a problem and you reach for XML… congratulations! Now you have TWO (2) problems.

In general I highly suggest staying away from Unity’s JSON “tiny lite” package. It’s really not very capable at all and will silently fail on very common data structures, such as Dictionaries and Hashes and ALL properties.

Instead grab Newtonsoft JSON .NET off the asset store for free, or else install it from the Unity Package Manager (Window → Package Manager).

https://assetstore.unity.com/packages/tools/input-management/json-net-for-unity-11347

Also, always be sure to leverage sites like:

https://jsonlint.com
https://json2csharp.com
https://csharp2json.io

One way is TextAsset which you can use as a field type.
You then literally dragndrop the file on this field, and boom, it’s natively accessible by your script.
If this is an editor, you can also use AssetDatabase.GetAssetPath() with this asset for more functionalities, but basically you can access its contents through ‘text’ and it will get embedded with your project automatically.

1 Like

I actually like XMLs a lot, moreso than JSON files. They are somehow more, uhm, honest.
But Kurt’s recommendation is, granted, better >>vastly superior in fact<< for most applications out there, yours is not as special.

Ok but then how do I access data stored on a JSON file? Any quick links you have to spare me having to sort through potentially unhelpful similar results?

I COULD use scriptable objects but from my cursory search that doesn’t seem to really have to do with what I’m trying; i.e. a lot of dialog lines to change a single TextObject (subtitle) and then objectives to populate per level

The crux of your problem really is about storing data in such a way that you can use it later. Scriptable Objects are the go-to for this in Unity as they’re a completely native way to design bags of data, with no deserialisation needed as Unity does it all for you.

So it’s pretty trivial to use scriptable objects to hold the data you want in a typical C# manner. Which, for dialogue, is often just a collection of strings.

Well hush my mouth. Thanks to you all