我正在try 使用XmlSerializer.Deserialize()作为我的XML文件. 这是我们的密码:

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!");
    }
}

这是对话课:

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

这是我正在使用的XML的一部分:

<?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>

其他的都很好,除了句子. 我从这个对话对象得到的数组是空的,而其他的像"oid"这样的数组是正常的. 我想这是因为"句子"是嵌套的,但我没有什么 idea 如何正确处理这个问题.

做了一些日志(log),找出了对话.句子是空的,其他的不是. 我想修改当前的代码,以便它能正确处理句子.

推荐答案

将dialog sentences属性更新为一个XMLArrayItem,元素名为'sentce':

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

Csharp相关问答推荐

如何打印已添加到List的Linq值,而不是C#中的:System.Collections.Generic.List ' 1[System.Int32]?

ASP.NET Core -是否可以对所有最小API端点应用过滤器?

PredicateBuilder不是循环工作,而是手动工作

一小时后,自定义缓存停止在App Insight中保存

. NET Core DB vs JSON模型设计

我需要两个属性类吗

从应用程序图API调用访问所有者字段

Blazor WebApp:原始异常:AADSTS700025:客户端是公共的,因此既不应显示客户端,也不应显示客户端

单元测试:模拟返回空

未在数据流块之间传播完成

DbContext-传递自定义配置选项

正在寻找新的.NET8 Blazor Web应用程序.如何将.js添加到.razor页面?

数据库.Migrate在对接容器重启时失败

有条件地定义预处理器指令常量

是否可以从IQueryable T中获取一个IdentyEntry T>

c#在后台实现类型化数组

如何在单击按钮后多次异步更新标签

Xamarin.Forms中具有类似AspectFill的图像zoom 的水平滚动视图

Xamarin Forms应用程序中登录页面的用户名和密码编辑文本之间不需要的空格

无法创建&Quot;&Quot;类型的实例,因为无法将一个或多个参数绑定到