Hi all – I’m pretty new to XML, but I have a working Item and Weapon system and was going to create a basic dialog system in the same way, but ran into an issue going “one level deeper” in the XML.
As a quick example of my problem, this works and I can access everything in it:
<NPCs>
<NPC name = "Bob">
<Description> A description for the NPC. </Description>
<Line>Hello!</Line>
</NPC>
</NPCs>
But I can’t access the event that is “one level down” here:
<NPCs>
<NPC name = "Bob">
<Description> A description for the NPC. </Description>
<Event eventName = "firstMeet">
<Line>Hello!</Line>
</Event>
<Event eventName = "alreadyMet">
<Line>Hello again!</Line>
</Event>
</NPC>
</NPCs>
I currently have it set up pretty simply – there is an Array of NPCs. Each NPC has a name attribute, and also has a description. NPCs also have different “events” that are exclusive to them and, on triggering that event, their dialog changes to the Lines listed under that eventName.
Xml document:
<?xml version="1.0" encoding="UTF-8"?>
<DialogCollection>
<NPCs>
<NPC name = "Bob" >
<Description>this is the npc descriptionr</Description> <!-- Descriptions may be used later for like a bestiary for npcs -->
<Event eventName = "ThisIsAnEvent">
<Line>Hey. </Line>
<Line>Hey 2. </Line>
<Line>Hey 3 </Line>
</Event>
<Event eventName = "test2">
<Line> Hey test. </Line>
<Line> Hey test 2. </Line>
<Line> Hey test 3. </Line>
</Event>
</NPC>
<NPC name = "Greg" >
<Description>this is the npc descriptionGREG</Description> <!-- Descriptions may be used later for like a bestiary for npcs -->
<Event eventName = "Greg">
<Line> Hey. </Line>
<Line> Hey 2. </Line>
<Line> Greg hey. </Line>
</Event>
<Event eventName = "Greg2">
<Line> Hey test. </Line>
<Line> Hey test 2. </Line>
<Line> Greg hey 2. </Line>
</Event>
</NPC>
</NPCs>
</DialogCollection>
I have a DialogContainer and EventContainer that will store this XML information. The DialogContainer is working fine – I can see each NPC’s name and description and assign it to different NPCs without issue. For events, I get a nothing. ![]()
Dialog and Event collection
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml.Serialization;
using System.IO;
[XmlRoot("DialogCollection")]
public class DialogContainer
{
[XmlArray("NPCs")]
[XmlArrayItem("NPC")]
public List<Dialog> dialogNPC = new List<Dialog>();
public static DialogContainer Load(string path)
{
TextAsset _xml = Resources.Load<TextAsset>(path);
XmlSerializer serializer = new XmlSerializer(typeof(DialogContainer));
StringReader reader = new StringReader(_xml.text);
DialogContainer dialogContainerLines = serializer.Deserialize(reader) as DialogContainer;
reader.Close();
return dialogContainerLines;
}
}
[XmlRoot("DialogCollection")]
public class EventContainer
{
//I presume this is the problem and have tried changing this in every way possible. :(
[XmlArray("NPC")]
[XmlArrayItem("Event")]
public List<Event> dialogEvents = new List<Event>();
public static EventContainer Load(string path)
{
TextAsset _xml = Resources.Load<TextAsset>(path);
XmlSerializer serializer = new XmlSerializer(typeof(EventContainer));
StringReader reader = new StringReader(_xml.text);
EventContainer eventLines = serializer.Deserialize(reader) as EventContainer;
reader.Close();
return eventLines;
}
}
I have a Dialog and Event class shown below. Again, I can pull the XmlAttribute “name” and get the element “Description” just fine, but cannot get the “eventName” or “Line”.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;
using System.Xml.Serialization;
public class Dialog
{
[XmlAttribute("name")]
public string _name { get; set; }
[XmlElement("Description")]
public string _description { get; set; }
}
public class Event
{
[XmlAttribute("eventName")]
public string _eventName { get; set; }
[XmlElement("Line")]
public string[] _line { get; set; }
}
To test this, I’ve added the following Debug lines into my DialogHolder script that is attached to the NPC. I get the correct NPC count (2, Bob and Greg) but get an Event count of 0.
DialogContainer dc = DialogContainer.Load(path);
EventContainer ec = EventContainer.Load(path);
Debug.Log(dc.dialogNPC.Count + " NPC count");
Debug.Log(ec.dialogEvents.Count + " events count");
If I rearrange my XML document and put “Event” directly underneath NPCs, I can read the eventName and Lines just fine. The only issue there is that I then can’t create unique events and lines for each NPC!
My ultimate goal is to do something like this in the DialogHolder script attached to an NPC:
DialogContainer dc = DialogContainer.Load(path);
EventContainer ec = EventContainer.Load(path);
foreach (Dialog dialogNPC in dc.dialogNPC)
{
if (dialogNPC._name == npcName)
{
npcDescriptoin = dialogNPC._description;
}
foreach (Event dialogEvents in ec.dialogEvents)
{
if (dialogEvents._eventName == eventName)
{
//change dialog lines to be used based on that Event's lines
}
}
But while I can get the NPC’s name and description, I can’t get the event or the lines associated with the event. What can I do to be able to access those child nodes (if that is the right terminology)? I’m sure I’m missing something and you can go “one layer down” into the XML, but I’m too inexperienced with XML to really know how.

