1

I'm trying to use XmlSerializer.Deserialize() for my XML file. This is the code:

private void LoadSceneDialogues()
{
    if (dialogueXML != null)
    {
        sceneDialogues = new Dictionary<int, Dialogue>();
        XmlSerializer serializer = new XmlSerializer(typeof(List<Dialogue>), new XmlRootAttribute("Dialogues"));
        using (var reader = new StringReader(dialogueXML.text))
        {
            try
            {
                Debug.Log("Purely for Debugging1");
                var loadedData = (List<Dialogue>)serializer.Deserialize(reader);
                foreach (var data in loadedData)
                {

                    Debug.Log($"oID: {data.oID} name: {data.characterName}");
                    foreach (var sentence in data.sentences)
                    {
                        Debug.Log($"sentence: {sentence}");
                    }
                    Debug.Log($"sentences count: {data.sentences.Length}");
                    //Debug.Log($"sentences: {data.sentences}");
                    sceneDialogues.Add(data.oID, data);
                }
                if (sceneDialogues != null)
                {
                    Debug.Log("Purely for Debugging2");
                }
            }
            catch (InvalidOperationException ex)
            {
                Debug.LogError($"Error deserializing XML: {ex.Message}");
                Debug.Log($"Loaded XML data:\n{dialogueXML.text}");
            }
        }
    }
    else
    {
        Debug.LogError("Dialogue XML file not assigned in DialogueManager!");
    }
}

This is dialogue class:

using UnityEngine;

[CreateAssetMenu(fileName = "NewDialogue", menuName = "Dialogue")]
public class Dialogue : ScriptableObject
{
    public string characterName; // 캐릭터 이름
    [TextArea(3, 20)]
    public string[] sentences; // 대화 내용
    public int oID = -1;
    public int nextoID = -1;
    public int animID = -1;
}

And this is a part of XML I'm using:

<?xml version="1.0" encoding="UTF-8"?>
<Dialogues>
   <Dialogue>
      <oID>0</oID>
      <nextoID>-1</nextoID>
      <animID>0</animID>
      <characterName>a</characterName>
      <sentences>
         <sentence>god...</sentence>
         <sentence>wtf?</sentence>
      </sentences>
   </Dialogue>
</Dialogues>

Others work fine, except for sentences. The array I get from this dialogue object is empty, while the others like "oid" is as they should. I think that's because "sentences" is nested one, but I have 0 idea how to handle this properly.

Did some logs and found out dialogue.sentences is empty, while others not. I want to modify current code so it handles sentences properly.

1 Answer 1

0

Update the dialog sentences property to be an XMLArrayItem with the element name of 'sentence':

    public class Dialogue : ScriptableObject
{
    public string characterName; // 캐릭터 이름 
  
    [XmlArrayItem(ElementName="sentence")]
    public List<string> Sentences{ get; set; }

    public int oID = -1;
    public int nextoID = -1;
    public int animID = -1;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.