【问题标题】:Loading simple XML file not working, educational game加载简单的 XML 文件不起作用,教育游戏
【发布时间】:2016-01-31 05:51:05
【问题描述】:

嗨,伙计们,我想你们或许可以帮助我。制作简单的教育问答类游戏,我卡在最后一步 > 我无法从 XML 文件加载数据。 这是一个xml文件的例子。

<QuestionData>
<Question>
<questionText>4+4?</questionText>
<answerA>1</answerA>
<answerB>2</answerB>
<answerC>8</answerC>
 <answerD>4</answerD>
<correctAnswer>8</correctAnswer>
 </Question>
 <Question> 
   .......(another question+answers)
 </Question>
  </QuestionData>

这应该加载 XML 数据

using UnityEngine;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System;

public struct Question {
public string questionText;
public string answerA;
public string answerB;
public string answerC;
public string answerD;
public string correctAnswerID; 
}

[XmlRoot]

public class QuestionData {
[XmlArray("questions")]
[XmlArrayItem("Question")]
public List<Question> questions = new List<Question>();

public static QuestionData LoadFromText(string text) {
    try {
        XmlSerializer serializer = new XmlSerializer(typeof(QuestionData));            
        return serializer.Deserialize(new StringReader(text)) as QuestionData;
    } catch (Exception e) {
        UnityEngine.Debug.LogError(e);
        return null;
    }
}
}

现在,这是我将数据附加到特定游戏对象的脚本。但是,xml数据的加载似乎根本不起作用。

public class QuestionLoading : MonoBehaviour {
[SerializeField]
public TextAsset questionDataXMLFile;
public QuestionData questionData;
public Question currentQuestion;
void Start() {


    questionData = QuestionData.LoadFromText(questionDataXMLFile.text);
    Debug.Log (questionData.questions.Count);
    Debug.Log (currentQuestion.questionText);
 }

当我调试 questionData.questions.Count 和 currentQuestion 时,我总是得到 0 值。知道为什么吗?

【问题讨论】:

    标签: xml unity3d xml-parsing


    【解决方案1】:

    尝试将 xml 对象更改为具有 xml 属性声明的类。我也没有看到你将currentQuestion 分配给任何东西。

    [XmlRoot(ElementName = "Question")]
    public class Question
    {
        [XmlElement(ElementName = "questionText")]
        public string QuestionText { get; set; }
        [XmlElement(ElementName = "answerA")]
        public string AnswerA { get; set; }
        [XmlElement(ElementName = "answerB")]
        public string AnswerB { get; set; }
        [XmlElement(ElementName = "answerC")]
        public string AnswerC { get; set; }
        [XmlElement(ElementName = "answerD")]
        public string AnswerD { get; set; }
        [XmlElement(ElementName = "correctAnswer")]
        public string CorrectAnswer { get; set; }
    }
    
    [XmlRoot(ElementName = "QuestionData")]
    public class QuestionData
    {
        [XmlElement(ElementName = "Question")]
        public List<Question> Question { get; set; }
    
        static public QuestionData LoadFromText(string text)
        {
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(QuestionData));
                return serializer.Deserialize(new StringReader(text)) as QuestionData;
            }
            catch (Exception e)
            {
                UnityEngine.Debug.LogError(e);
                return null;
            }
        }
    }
    

    【讨论】:

    • 谢谢,这解决了我的问题 :) 现在一切正常...真的非常感谢!
    猜你喜欢
    • 2011-01-25
    • 1970-01-01
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多