Heya everyone,
I’ve been working a class that’s supposed to Deserialize my scene’s dialog from an XML file. This is done by having a “SceneDialog”, containing a list of "Dialog"s, which contains a list of "DialogLine"s, which contains a list of "Response"s. (SceneDialogs > Dialog > DialogLine > Response). However, while it correctly serializes SceneDialogs and a list of Dialogs, it stops there and doesn’t add the list of DialogLines to the Dialog.
This is the class the deserializes the xml-file; this seems to work perfectly fine.
public static T Deserialize<T> (string path) {
XmlSerializer serializer = new XmlSerializer (typeof(T));
StreamReader reader = new StreamReader (path);
T deserialized = (T)serializer.Deserialize (reader.BaseStream);
reader.Close ();
return deserialized;
}
The following Lines call the Deserialize () function, and the classes the data is supposed to be stored in.
//Some stuff to check if the file path is valid
if (File.Exists (Path.Combine (Application.dataPath, xmlPath))) {
sceneDialogs = XML_Loader.Deserialize <SceneDialogs> ((Path.Combine (Application.dataPath, xmlPath)));
} else {
Debug.LogError ("XML File: " + (Path.Combine (Application.dataPath, xmlPath)) + " is not found.");
}
[XmlRoot ("SceneDialogs")]
[XmlInclude(typeof(Dialog))]
[XmlInclude(typeof(DialogLine))]
[XmlInclude(typeof(Response))]
public class SceneDialogs {
[XmlArray("RandomNode"), XmlArrayItem("Dialog")]
public List<Dialog> allDialogsInScene;
}
[XmlType ("Dialog")]
public class Dialog {
[XmlAttribute ("ID")]
public int ID;
[XmlArray("Dialog"), XmlArrayItem("DialogLine")]
public List <DialogLine> DialogLines;
}
[XmlType ("DialogLine")]
public class DialogLine {
[XmlAttribute ("lineID")]
public int lineID;
[XmlElement ("DialogText")]
public string text;
[XmlArray("DialogLine"), XmlArrayItem("Response")]
public List <Response> responses;
}
[XmlType ("Response")]
public class Response {
[XmlAttribute ("responseID")]
public int responseID;
[XmlElement ("ResponseText")]
public string text;
[XmlElement ("FollowUpLine")]
public int followupLine;
}
As for the actual XML file in question:
<?xml version="1.0" encoding="UTF-8"?>
<SceneDialogs>
<RandomNode>
<Dialog ID='0000'>
<DialogLine lineID='0'>
<DialogText> "Stay a while and listen." </DialogText>
<Response responseID='0'>
<ResponseText> "Okay." </ResponseText>
<FollowUpLine> '1' </FollowUpLine>
</Response>
<Response responseID='1'>
<ResponseText> "No." </ResponseText>
<FollowUpLine> '-1' </FollowUpLine>
</Response>
</DialogLine>
<DialogLine lineID='1'>
<DialogText> "Once upon a time, I was like... the main character of this scene." </DialogText>
<Response responseID='0'>
<ResponseText> "Wow, that's amazing!." </ResponseText>
<FollowUpLine> '3' </FollowUpLine>
</Response>
<Response responseID='1'>
<ResponseText> "I don't believe you." </ResponseText>
<FollowUpLine> '-1' </FollowUpLine>
</Response>
</DialogLine>
</Dialog>
</RandomNode>
</SceneDialogs>
As you can see, it correctly identifies and creates the SceneDialogs, and places one Dialog in it with the correct ID. It does not, however add any of the DialogLines into the Dialog’s List (the count stays 0). To add to this, the entire XmlSerializer is kind of a blackbox, which does something but there’s no real method of tracking what’s going on inside.
So, if anyone has any idea on what is going wrong and how to fix it I would greatly appreciate it.
Thank you in advance.