• 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 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
Add comment · 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

· Add your reply
  • Sort: 
avatar image
0

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

thanks for your help!!

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

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

JSON vs XML for Unity C# 1 Answer

Distribute terrain in zones 3 Answers

How to find the name of an xml I am opening 1 Answer

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