• 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 rishabhvinod · Sep 04, 2013 at 08:55 AM ·

how can i parse xml through API in unity please can anyone explain with some example.

I am trying to parse an XML through API . XML is loading but unable to read nodes of the xml, please can anyone help me with some suitable example from which i can read nodes of my XML and display in GUI.

Thanks in advance

Comment
Add comment · Show 1
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 rishabhvinod · Sep 04, 2013 at 10:07 AM 0
Share

please guys help me out....

2 Replies

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

Answer by rishabhvinod · Sep 04, 2013 at 12:27 PM

Hi Guys i got the solution and i am shring with you all for reference....

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

public class newLoad : MonoBehaviour {

 private string _url = "your API or url of xml";

 private string _xml;
 Vector2 scrollPosition = Vector2.zero;
 XmlDocument xmlDoc = new XmlDocument();
 List<Dictionary<string,string>> levels = new List<Dictionary<string,string>>();
 Dictionary<string,string> obj;
 ArrayList listItems = new ArrayList();
 
 IEnumerator Start() {

      // Start a download of the given URL

     WWW www = new WWW(_url);
     
 
     // Wait for download to complete

     yield return www;
     
     // get text

     _xml = www.text;

     
     XmlDocument xmlDoc = new XmlDocument();
      xmlDoc.LoadXml(www.text);

      Debug.Log(_xml);
     print(xmlDoc);

     // having XML here. Do with it whatever you want...
             
     XmlNodeList levelsList = xmlDoc.GetElementsByTagName("MainCategories");
     
     foreach (XmlNode levelInfo in levelsList)
       {
         //Debug.Log("foreach inner");
         string str = levelInfo.InnerXml;
            XmlNodeList levelcontent = levelInfo.ChildNodes;
            obj = new Dictionary<string,string>();
     //    Debug.Log("MainCategories = " + str);
     
         foreach (XmlNode levelsItens in levelcontent)
            {
             //Debug.Log("~~~~~~First Node~~~~~");
             Dictionary<string,string> obj1 = new Dictionary<string,string>();
             if(levelsItens.Name == "MainCategory")
             {
                 //Debug.Log("~~~~~~Second Node~~~~~");
                 string str1 = levelsItens.Name;
                  obj1.Add("MainCategory",levelsItens.InnerText);
             //    Debug.Log("MainCategory = " + str1);
         
                 foreach (XmlNode levelsItens1 in levelsItens)
                    {
                     //Debug.Log("~~~~~~Third Node~~~~~");                        
                     Dictionary<string,string> obj2 = new Dictionary<string,string>();
                     if(levelsItens1.Name == "MainCategoryID")
                     {
                         string str2 = levelsItens1.InnerXml;
                          obj2.Add("MainCategoryID",levelsItens1.InnerText);
                         Debug.Log("MainCategoryID = " + str2);
                     }
                     if(levelsItens1.Name == "MainCategoryTitle")
                     {
                         string str3 = levelsItens1.InnerXml;
                          obj2.Add("MainCategoryTitle",levelsItens1.InnerText);
                         Debug.Log("MainCategoryTitle = " + str3);
                         
                         if(str3 != "NULL"){
                             
                             listItems.Add(str3);
                         }
                         
                     }                    
                 }
             }    
         }
     }
 }

 void OnGUI () {
     
    GUILayout.BeginArea(new Rect(0f, 0f, 300f, 200f), GUI.skin.window);
    scrollPosition = GUILayout.BeginScrollView(scrollPosition, false, true); 
    GUILayout.BeginVertical(GUI.skin.box);
 
    foreach (string item in listItems)
    {
      GUILayout.Label(item, GUI.skin.box, GUILayout.ExpandWidth(true));
    }
 
    GUILayout.EndVertical();
    GUILayout.EndScrollView();
    GUILayout.EndArea();
 }
 
Comment
Add comment · 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
0

Answer by sooncat · Sep 04, 2013 at 10:34 AM

Import System.xml.dll in "Assets/Plugins" folder.

By the way, xmlDll is too big somtime. Json may be a better choice.

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 rishabhvinod · Sep 04, 2013 at 11:00 AM 0
Share

@sooncat thanx for reply.. but i have System.xml.dll in my "Assets/Plugins" folder.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to get in game numerical input 1 Answer

whole animation not being played 0 Answers

Mid point of a object 1 Answer

Create Circular Motion to follow the object 2 Answers

only detect numerical keyboard input 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges