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 mmangual_83 · Feb 27, 2014 at 09:08 PM · c#xmldeserialization

Learning how to deserialize xml file

I am VERY new to read/write from a file, especially in Unity. I want to know how do I take the contents of an xml file, like oh say:

 <?xml version="1.0" encoding="UTF-8"?>
 
 -<DataContainer xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <userID>2497</userID>
 
 <Name>John</Name>
 
 <age>28</age>
 
 <Hobby>Painting</Hobby>
 
 <Career>Carpenter</Career>
 
 <Name>Andrea</Name>
 
 <age>18</age>
 
 <Hobby>Music</Hobby>
 
 <Career>Student</Career>
 
 
 <Name>Rick</Name>
 
 <age>31</age>
 
 <Hobby>Swimming</Hobby>
 
 <Career>Lifeguard</Career>
 
 </DataContainer>



Then in a class in my project I am supposed to load this file from the xml. So far I managed to create the class and declare the variables I need top get the job done (or at least I hope I do)

 using UnityEngine;
 using System;
 using System.IO;
 using System.Xml;
 using System.Text; 
 using System.Collections;
 using System.Xml.Serialization;
 using System.Collections.Generic;
 
 public class UserData : MonoBehaviour
 {
     #region Properties
     [XmlAttribute("UserID")]
     public List<int> _userID { get; private set; }
     [XmlAttribute("Name")]
     public List<string> _Name { get; private set; }
     [XmlAttribute("Age")]
     public List<int> _Age { get; private set; }
     [XmlAttribute("Hobby")]
     public List<string> _Hobby { get; private set; }
     [XmlAttribute("Career")]
     public List<string> _Career { get; private set }
     #endregion
 
     public BrowseUserData()
     {
         _userID = new List<int>();
         _Name = new List<string>();
         _Age new List<int>();
         _Hobby = new List<string>();
         _Career = new List<string>();
     }
 
     /// <summary>
     /// This function searches for the user files  
     /// </summary>
     public void OpenUserFiles(String xmlFile)
     {
         if (File.Exists(xmlFile))
         {
         }
         else
             Debug.LogError("Failed to find correct file path");
     }
      
 }


That is how far I managed to get using this tutorial

but I get lost with my next step which is how to load each xml element into its respective variable.

Can anyone help me figure out if I am doing this correctly and how to read from the xml file correctly? Many Thanks in advance!

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
1
Best Answer

Answer by ArkaneX · Feb 27, 2014 at 09:59 PM

I answered similar question in the past, and trying to help I discovered a decent tutorial. I suggest reading it: part 1, part 2.

As a side note - if you plan to use your class in serialization, don't derive it from MonoBehaviour. Additionally, there's an error in your code, because constructor name doesn't match class name.

Comment
Add comment · Show 5 · 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 mmangual_83 · Feb 27, 2014 at 10:22 PM 0
Share

Yes, I took away the $$anonymous$$onodevelop part that was stupid. Right now I have a method where I just handle the parsing it looks like this:

     public void Parse(string xmlfile)
     {
         // location of X$$anonymous$$L file 
         string strXmlDoc = xmlfile;
 
         // create an instance of XmlDocument object
         XmlDocument objXmlDoc = new XmlDocument();
 
         // load X$$anonymous$$L document in XmlDocument object
         objXmlDoc.Load(strXmlDoc);
 
         // get the root element 
         XmlElement objRootElem = objXmlDoc.DocumentElement;
 
         // get more details about the node
         Debug.Log(objRootElem);
     }


How do I start telling the program: "Start filling out the list with their respective xml elements"

avatar image ArkaneX · Feb 27, 2014 at 11:33 PM 0
Share

You don't need XmlDocument for this. You have to use XmlSerializer class. I really suggest to take a look at the tutorial I linked, especially second part. After reading first few paragraphs, you should be able to accomplish your task.

There's one catch though: please note, that author of the tutorial uses class name matching X$$anonymous$$L root name, and property names matching X$$anonymous$$L element names (this is case sensitive). In your case, you either have to ensure such matching, or you have to use XmlElement and XmlRoot attributes, to tell serializer what are the names of your X$$anonymous$$L nodes. These attributes are described in the first part of tutorial.

I've just noticed, that you used XmlAttribute attributes - these are required if you want to map your properties to attributes in xml file, while you want to map to elements.

avatar image HydroxTV ArkaneX · Apr 10, 2016 at 08:48 PM 0
Share

I know this answer is quite old,but I just hope ArkaneX you are still active here on the forums. Would you be so kind and give us a working example how to deserialize a xml file like:

 <buildings>
 <building>House</building>
 </buildings>


Ive been googling for hours, found your given tutorial as well but I just cant get it to work. I am very new to C# as well as Unity so translating it from "normal C#" to "unity C#" is quite hard for me. But I really want to start with Serializing and deserializing because it will be a big part of my game :) Thanks in advance.

avatar image ArkaneX HydroxTV · Apr 10, 2016 at 09:46 PM 1
Share

It would be best to include a description of the problems you encountered, plus xml and code you tried. I suggest posting a new question, because more users will be able to help then. After posting, you could add a comment here with link to your question, so I can take a look at it.

When you add xml or code to a post, please remember to format it, using a toolbar on top of the edit field. It looks like you tried to include some xml in your comment, but it disappeared, because it wasn't formatted properly.

Show more comments

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

22 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

C# Deserializing XML and Assigning Values 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


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