Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 AmunTech · Aug 09, 2013 at 12:32 PM · c#arrayxml

create string array from xml

i need to create an array of strings from an xml file on a server:

          IEnumerator Start(){
         
         string XMLPareti = "http://www.sblinko.com/store/index.php?controller=categories";
         WWW www = new WWW(XMLPareti);
      
         //Load the data and yield (wait) till it's ready before we continue executing the rest of this method.
         yield return www;
         if (www.error == null){
           //Sucessfully loaded the XML
           Debug.Log("Loaded following XML " + www.data);
      
           //Create a new XML document out of the loaded data
           //XmlDocument xmlDoc = new XmlDocument();
          // xmlDoc.LoadXml(www.data);
                 XElement paretiXml = XElement.Load(XMLPareti);
                 
                 
                 IEnumerable<string> llinks = from pLink in paretiXml.Elements ("category")
                                                 let urls = (string)pLink.Element("link")
                                                 orderby urls
                                                 select urls;
                 
                 
                 Links = new ArrayList();
                 
                 foreach (string lk in llinks){
                     
                     Debug.Log(lk);
                     
                     Links.Add (lk);
                     
                     String[] paretiToLoad = (String[]) Links.ToArray( typeof( string ));
                     ParetiToLoad = paretiToLoad;
                     
                     Debug.Log(ParetiToLoad.Length);
                 }
                     
             }else{
                 
               Debug.Log("ERROR: " + www.error);
         }
             
       }
     
         

The problem is when i try to use the data from the created array ParetiToLoad, i can see only one item but in console show me all needed nodes. How can i add all the nodes "link" inside the string array? Thanks in advance for any help!!

Comment
Add comment · Show 11
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 sacredgeometry · Aug 09, 2013 at 12:46 PM 0
Share

Out of curiosity and to better understand the task, can I ask what the purpose of converting the xml into an array of strings is?

avatar image AmunTech · Aug 09, 2013 at 01:22 PM 0
Share

I'm creating a store and I have to populate the gui, descriptions, images, etc., with an xml file. Create the array with the data taken from the xml it seemed the most practical way to use the data, but I am open to any advice.. i'm new in the use of X$$anonymous$$L..

avatar image sacredgeometry · Aug 09, 2013 at 10:43 PM 0
Share

There are a number of ways dependent on the need and comfort. You could use xpath to navigate but this can sometimes be clunky. Personally I would use LINQ, basically it allows you to iterate and navigate through xml as if you were using objects.

heres a quick example

http://www.dotnetcurry.com/ShowArticle.aspx?ID=564

and heres the reference

http://msdn.microsoft.com/en-us/library/system.xml.linq.aspx

avatar image sacredgeometry · Aug 09, 2013 at 11:37 PM 0
Share

Let me know if that helps, if not I can help you do it the traditional way. Even so it might be worth writing a class for the nodes and attributes .. especially if theres a generic template you can create one from :)

avatar image AmunTech · Aug 14, 2013 at 05:22 PM 0
Share

Thanks a lot, linq seems to be easier. Have any limitations? What are the advantages of the traditional way? Thanks for the help

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by whydoidoit · Aug 19, 2013 at 08:59 PM

I'd suggest scripting classes to map to your data structure and then using XmlSerializer to recreate an object graph:

   [XmlRoot("rootTag")]
   public class SomeClass
   {
        [XmlAttribute] public string anAttribute;
        [XmlElement] public int anElement;
        [XmlArray][XmlArrayItem("thing")] Thing things;
   }

   [XmlRoot("thing")]
   public class Thing
   {
         [XmlElement] public string element;
         [XmlAttribute] public int integer;
   }

Which supports Xml formatted as:

  <rootTag anAttribute="This is me">
        <anElement>1</anElement>
        <things>
            <thing integer = "1">
                  <element>Hello</element>
            </thing>
            <thing integer = "2">
                  <element>World</element>
            </thing>
        </things>
  </rootTag>

Then you could use this utility code:

 using UnityEngine;
 using System.Collections;
 using System.Xml.Serialization;
 using System.IO;
 using System.Text;
 using System;
 
 public static class XmlSupport 
 {
     public static T DeserializeXml<T> (this string xml) where T : class
     {
         if( xml != null ) {
             var s = new XmlSerializer (typeof(T));
             using (var m = new MemoryStream (Encoding.UTF8.GetBytes (xml)))
             {
                 return (T)s.Deserialize (m);
             } 
         }
         return null;
     }
     
     public static object DeserializeXml(this string xml, Type tp) 
     {
         var s = new XmlSerializer (tp);
         using (var m = new MemoryStream (Encoding.UTF8.GetBytes (xml)))
         {
             return s.Deserialize (m);
         }
     }
 
     
     public static string SerializeXml (this object item)
     {
         var s = new XmlSerializer (item.GetType ());
         using (var m = new MemoryStream())
         {
             s.Serialize (m, item);
             m.Flush ();
             return Encoding.UTF8.GetString (m.GetBuffer ());
         }
         
         
     }
     
 }


And to get a SomeClass do this:

    var instance = someStringOfXml.Deserialize<SomeClass>();
    Debug.Log(string.Format("{0} has {1} things", instance.anAttribute, instance.things.Length));

See these references:

XmlAttributeAttribute

XmlElementAttribute

Xml Serialization Namespace

XmlSerializer

You can of course use Linq on the objects returned by this method.

Comment
Add comment · Show 2 · 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 AmunTech · Aug 21, 2013 at 01:35 PM 0
Share

Probably this is the way i'll try thanks

avatar image AmunTech · Aug 26, 2013 at 02:42 PM 0
Share

I'm trying your suggestion. when i call someclass, what is someStringOfXml? I haven't nothing that contains definition for Deserialize. Thanks for the help

avatar image
0

Answer by Eclipsed · Aug 09, 2013 at 01:01 PM

Well, on line 22 you're overwriting the "ParetiToLoad" array for each entry in the xml.

This looks like an error, but I should check the xml structure to fully understand what you're trying to do ;)

Cheers.

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

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.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Trouble updating old Unity syntax 0 Answers

C#-XMLSerialize a Struct with Vector3 Array in it 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How can i play with text which have string and int (from XML) 2 Answers


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