- Home /
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!
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.
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"
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.
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.
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613134936im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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