How to load specific data from XMLArray
Hi so my goal here is to make a little quiz game thing and try and load the question data from an external xml file. (I get how to do the external thing, just haven't done it yet)... Right now I'm having a problem with getting the options for a certain question to load into the 4 UI buttons. My XML Parser is working, I can import the data of the question itself and display the current question count and also I can get the correct answer to print in console, but I've simply hit a brick wall about where to go from here. The is my first time working with XML data so sorry if I'm super noobish with it! Attached is my test Question Data XML File.
Here is my QuestionContainer.cs:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
[XmlRoot("WalkThePlank")]
public class QuestionContainer {
[XmlArray("Questions")]
[XmlArrayItem("Question")]
public List<Question> questions = new List<Question> ();
public static QuestionContainer Load(string path)
{
TextAsset _xml = Resources.Load<TextAsset>(path);
XmlSerializer serializer = new XmlSerializer (typeof(QuestionContainer));
StringReader reader = new StringReader(_xml.text);
QuestionContainer questions = serializer.Deserialize (reader) as QuestionContainer;
reader.Close ();
return questions;
}
}
Here is Question.cs:
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
using UnityEngine;
public class Question {
[XmlAttribute("number")]
public int number;
[XmlElement("Piratequestion")]
public string piratequestion;
[XmlElement("Option")]
public string Option;
}
Here is QuestionUI.cs:
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class QuestionUI : MonoBehaviour {
public const string path = "questionData";
public int currentQuestion;
public Text questionText;
public Text button1Text;
public Text button2Text;
public Text button3Text;
public Text button4Text;
public Text questionsCountText;
public int totalQuestions;
public int remainingQuestions;
// Use this for initialization
void Start () {
currentQuestion = 1;
QuestionContainer qc = QuestionContainer.Load(path);
totalQuestions = qc.questions.Count;
remainingQuestions = totalQuestions;
foreach (Question question in qc.questions)
{
if (question.number == currentQuestion)
{
questionText.text = question.piratequestion;
foreach (Question option in qc.questions)
{
print (question.Option);
}
}
}
}
// Update is called once per frame
void Update ()
{
questionsCountText.text = "Question " + currentQuestion.ToString () + "/" + totalQuestions.ToString ();
}
}
I'm doing most of my work in QuestionUI so its a partially completed script, no idea what to do now to get it to work... please help, I'm a desperate man <3
Was your question answered, though this posted long time ago,
Was also thinking about almost the same. Im no coder and dont know c#, but my idead was if there is no internet or offline, use the current xml file if online fetch the xml from the server.
I got a free True or False asset and the xml works great, just want to add that line.
Your answer
Follow this Question
Related Questions
Random XML Questions Using Fisher-Yates Shuffle 0 Answers
Randomize text position for 2D Quiz C# 0 Answers
List resulting in out of range 0 Answers
xml file read as MonsterCollection.cs 0 Answers