hey all, below is a working script for using an xml file and reading each of it’s nodes ![]()
am not sure if something like this is already posted
using UnityEngine;
using System.Collections;
using System.Xml;
using System.IO;
using System.Text;
public class xmlTest : MonoBehaviour {
// assuming i have this xml
string xmlString = "<?xml version=\"1.0\" ?> " +
"<school>" +
"<student>" +
"<id>1</id>" +
"<Fname>john</Fname>" +
"<Lname>last name</Lname>" +
"</student>" +
"<student>" +
"<id>2</id>" +
"<Fname>lily</Fname>" +
"<Lname>something</Lname>" +
"</student>" +
"</school>";
// Use this for initialization
void Start ()
{
ReadXmlData(xmlString );
}
void ReadXmlData(string XmlDataString)
{
using (XmlReader reader = XmlReader.Create(new StringReader(XmlDataString)))
{
reader.MoveToContent();
if (reader.ReadToDescendant("student"))
{
do
{
reader.ReadToDescendant("id");
reader.ReadStartElement("id");
print("id : " + reader.Value);
reader.ReadToFollowing("Fname");
reader.ReadStartElement("Fname");
print("first name: " + reader.Value);
reader.ReadToFollowing("Lname");
reader.ReadStartElement("Lname");
print("last name: " + reader.Value);
}
while (reader.ReadToFollowing("student"));
}
}
}
}