• 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
Question by fadihasrouni · Jul 07, 2014 at 12:21 PM · c#xmlparsingxmlserializer

XML parsing c# in unity

Hello,

I am new to parsing xml using c# in unity, and i just want to ask what is the best way to parse an xml file in the assets folder.

i was doing some researches and found out 2 ways to do it:

  -the first using serialization (using System.Xml.Serialization)
  -the second using XmlNodes for each node and place data manually in an object, here is a code blow:
   

  

 public team [] getTeamsFromXml(){
         int i = 0;
         team [] teams = new team[2];
         XmlNodeList teamsNode = doc.SelectNodes ("MM_MTracker/match/teams/team");
 
         foreach (XmlNode node in teamsNode) {
             team tm = new team();
             tm.id = int.Parse (node.SelectSingleNode ("id").InnerText);     
             tm.label = node.SelectSingleNode ("label").InnerText;     
             tm.flag =  node.SelectSingleNode ("flag").InnerText;     
             tm.direction =  int.Parse ( node.SelectSingleNode ("direction").InnerText     );
 
             teams[i] = tm;
             i++;
       
         }
 
         return teams;
 
 
     }

What is the best methode and is there any more options?

Thanks in advance.

Comment

People who like this

0 Show 2
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 meat5000 · Jul 07, 2014 at 12:21 PM 1
Share

Its hard for us to answer "Best Way" questions. We can help you make it work but answers based on opinion don't really fit in to UA. If it works, go with it. Also this is verging on divulgence of trade secrets :)

avatar image fadihasrouni · Jul 07, 2014 at 12:56 PM 0
Share

yes it works fine with me. But i want to consider memory consumption as well. What does "Also this is verging on divulgence of trade secrets" means?

1 Reply

  • Sort: 
avatar image

Answer by Tiras69 · Jul 07, 2014 at 01:14 PM

Hi ! I think you should use System.Xml.serialization this allow you to create model classes. here a tutorial. in your case you will have something like that :

 [XmlRoot("root")]
 public class XmlFile{
    
    [XmlArray("teams")]
    [XmlArrayItem("team", typeof(Team))]
    public List<Team> Teams { get; set; }
 
    public XmlFile(){
       Teams = new List<Team>();
    }
 
 }
 
 [XmlRoot("team")]
 public class Team{
 
    [XmlAttribute("id")]
    public string Id { get; set; }
    
    [XmlAttribute("label")]
    public string Label { get; set; }
 
    [XmlAttribute("flag")]
    public string Flag { get; set; }
 
    [XmlAttribute("direction")]
    public string Direction { get; set; }
 }


But, It's my personnal choice. I use this method to make a big data-driven project.

Comment

People who like this

0 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 fadihasrouni · Jul 07, 2014 at 01:23 PM 0
Share

thanks for your help!!

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Invalid Encoding Specification Xml 1 Answer

C# Deserializing XML and Assigning Values 1 Answer

Xml Serialization? (how to read/write?) 1 Answer

IsolatedStorageException: Could not find a part of the path 0 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