• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ivan2506 · May 02, 2016 at 06:38 PM · xmlxmlserializer

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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 "=".

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ivan2506 · May 05, 2016 at 04:39 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Confusion with process of XML setup 0 Answers

Parse XML to Vector3 (and other objects you don't control) 0 Answers

Problems with the XML Serializer 1 Answer

unity and xml question 1 Answer

Need Help With XML Reading Class Initialization 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges