- Home /
loading XML quiz
in here i have code about simple quiz using xml as database
using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Text; using System.Xml; using System.IO;
public class dbxml : MonoBehaviour {
static int actualLevel=1;
static int LevelMaxNumber;
static int WaipointCounter;
static string JawabanA_Character;
static string JawabanB_Character;
static string JawabanC_Character;
static string JawabanD_Character;
private string finaltext= "";
public GameObject LevelName;
public GameObject Soal_GUI;
public GameObject jawaban_GUI;
public TextAsset GameAsset;
List<Dictionary<string,string>> levels = new List<Dictionary<string,string>>();
Dictionary<string,string> obj;
// Use this for initialization
void Start () {
GetLevel ();
StartCoroutine(LevelStartInfo(1.0F));
LevelMaxNumber = levels.Count;
}
// Update is called once per frame
//void Update () {
//}
public void GetLevel()
{
XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.LoadXml (GameAsset.text);
XmlNodeList levelsList= xmlDoc.GetElementsByTagName("levels");
foreach (XmlNode LevelInfo in levelsList)
{
XmlNodeList levelcontent = LevelInfo.ChildNodes;
obj= new Dictionary<string,string >();
foreach(XmlNode levelIsItens in levelcontent)
{
if(levelIsItens.Name="name")
{
obj.Add("name", levelIsItens.InnerText);
}
if(levelIsItens.Name="soal")
{
obj.Add("soal",levelIsItens.InnerText);
}
if(levelIsItens.Name="jawaban")
{
switch(levelIsItens.Attributes["name"].Value)
{
case "jawabanA": obj.Add("jawabanA", levelIsItens.InnerText);break;
case "jawabanB": obj.Add("jawabanB", levelIsItens.InnerText);break;
case "jawabanC": obj.Add("jawabanC", levelIsItens.InnerText);break;
case "jawabanD": obj.Add("jawabanD", levelIsItens.InnerText);break;
}
}
}
levels.Add(obj);
}
}
IEnumerator LevelStartInfo(float Wait)
{
string lvlName = "";
levels [actualLevel - 1].TryGetValue ("name", out lvlName);
string soal = "";
levels [actualLevel - 1].TryGetValue ("soal", out soal);
string jawaban = "";
levels [actualLevel - 1].TryGetValue ("jawabanA", out jawaban);
levels [actualLevel - 1].TryGetValue ("jawabanB", out jawaban);
levels [actualLevel - 1].TryGetValue ("jawabanC", out jawaban);
levels [actualLevel - 1].TryGetValue ("jawabanD", out jawaban);
GameObject LevelName = Instantiate (LevelName) as GameObject;
LevelName.GetComponent<GUIText> ().text = lvlName;
GameObject Soal_GUI = Instantiate (Soal_GUI) as GameObject;
Soal_GUI.GetComponent<GUIText> ().text = soal;
for (i=0; i<jawaban; i++)
{
GameObject jawaban_GUI=Instantiate(jawaban_GUI) as GameObject;
jawaban_GUI.GetComponent<GUIText>().text=jawaban;
}
}
}
I have error in foreach XML node, the error say 'Property or indexer 'system.XMLNode.Name' cannot be assigned to(it is read only'
so how to fix this?
Answer by jgodfrey · May 02, 2016 at 10:59 PM
This (and others like it):
if(levelIsItens.Name="name")
Should be:
if (levelIsItens.Name == "name")
Note the "==" instead of "=".
i have this code, but when I run this code it can load data from xml into game object using GUIText.. so what's is the problem?
Your answer
Follow this Question
Related Questions
How to type Color32 format into XML? 1 Answer
Why am I geting null reference exception while reading xml file? 0 Answers
"Root Element Missing: XML Exception" when trying to load data from XML file 2 Answers
XML Serialization not working on WebGL 0 Answers
[iOS]UnauthorizedAccessException: Access to path error 0 Answers