I’m trying to get my program to read an XML file from my other program, but it fails to load text info from the XML file.
The script can detect the XML file (and its contents) just fine, but I need help getting the text from the XML file to my program.
Here’s the XML file for reference:
<GameInformation>
<ProgramInfo>
<nameInfo> Nameing = "Program Title"</nameInfo>
<coderInfo> Coder = "By me"</coderInfo>
<versionInfo> Version = "1"</versionInfo>
<descriptionInfo> Description = "This is an XML file I am trying to get my program to read.</descriptionInfo>
</ProgramInfo>
</GameInformation>
And here’s my script also for reference:
using UnityEngine;
using UnityEngine.UI;
using System.Xml.Serialization;
using System.IO;
using System.Text;
using System;
using System.Xml;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public class GameInfo {
[XmlIgnore]/*[System.NonSerialized]*/
//public GameObject muzzleflashObject;
[XmlAttribute("nameInfo")] public string Nameing = "";
[XmlAttribute("coderInfo")] public string Coder = "";
[XmlAttribute("descriptionInfo")] public string Description = "";
[XmlAttribute("versionInfo")] public int Version = 1;
}
[System.Serializable]
[XmlRoot("GameInformation")]
public class InfoDirectory {
[XmlElement("ProgramInfo")]
public GameInfo[] GameInformation;
}
public class XMLTest : MonoBehaviour {
public string XmlPrint;
public Text GameTitleText;
public GameObject TitleTempStorage;
public Component Curr;
public Dictionary<string,GameInfo> InfoDictionary;
void Start(){
ReadGameInfo();
Debug.Log ("Did it work?");
}
void ReadGameInfo() {
System.IO.FileStream fileStream;
XmlReader reader;
InfoDictionary = new Dictionary<string,GameInfo>();
string path = "C:/GameInfo.xml";
GameTitleText = GameObject.Find("GameTitle").GetComponent<Text>();
TitleTempStorage = GameObject.Find("TitleTempStorage");
Curr = TitleTempStorage.GetComponent<CurrentGameTitle>();
var xmlSerializer = new XmlSerializer(typeof(InfoDirectory));
var stream = File.Open("C:/GameInfo.xml", FileMode.Open, FileAccess.Read);
var deserializedInfo = xmlSerializer.Deserialize(stream) as InfoDirectory;
reader = new XmlTextReader(stream);
stream.Close();
Debug.Log(deserializedInfo.GameInformation.Length);
for(int i = 0; i < deserializedInfo.GameInformation.Length; i++) {
GameInfo ProgramInfo = deserializedInfo.GameInformation*;*
-
XmlPrint = "This is " + (ProgramInfo.Nameing) + ", made by " + (ProgramInfo.Coder);*
-
GameTitleText.text = (ProgramInfo.Coder);*
-
Debug.Log ("Well, did it? " + ProgramInfo.Description);*
-
//(ProgramInfo.name, ProgramInfo); //-Not in use.*
}
}
}
If you know what’s wrong, please let me know. None of the answers on here has helped me so far and I want to get this part of my program over with (I’ve been trying to figure out how to display text from an XML file for nearly 4+ days already.) and continue with the rest of my program. Thanks!