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!