- Home /
Xml parser through C sharp
<color>Green</color>
<score>1000</score>
<size>2</size>
<color>RED</color>
<score>1000</score>
<size>1.5</size>
<color>Blue</color>
<score>1000</score>
<size>1</size>
this is my xml file i want to get the the score values using UnityEngine; using System.Collections; using System.Xml;
public class Parsing : MonoBehaviour {
public static int[] Values=new int[3];
void Start()
{
XmlDocument newone=new XmlDocument();
newone.Load("new.xml");
XmlNodeList xnlist1=newone.SelectNodes("/spheres/sphere1/score");
XmlNodeList xnlist2=newone.SelectNodes("/spheres/sphere2/score");
XmlNodeList xnlist3=newone.SelectNodes("/spheres/sphere3/score");
foreach(XmlNode xn1 in xnlist1)
{
Values[0]=int.Parse(xn1.InnerText);
}
foreach(XmlNode xn2 in xnlist2)
{
Values[1]=int.Parse(xn2.InnerText);
}
foreach(XmlNode xn3 in xnlist3)
{
Values[2]=int.Parse(xn3.InnerText);
}
Debug.Log(Values[0]);
Debug.Log(Values[1]);
Debug.Log(Values[2]);
}
} this parsing i m doing its working correct in unity but when i build it and try to run on web my Values[0],Values[1] and Values[2] goes to 0.... if anyone can help me....
<color>Green</color>
<score>1000</score>
<size>2</size>
<color>RED</color>
<score>1000</score>
<size>1.5</size>
<color>Blue</color>
<score>1000</score>
<size>1</size>
You need to use code formatting for xml to show properly. Also, see http://answers.unity3d.com/questions/139834/how-do-i-load-an-xml-file.html
Answer by It_is_hot_in_vegas · Oct 09, 2017 at 01:32 AM
I know I'm late, but the reason why this does not work is because the script is telling the program to save/read data from your computer, but when you run it on a web player(HTML, etc.), that ability to read/write usually becomes disabled. This happens because anti-virus software is preventing this read/write connection from being made, or the fact that the script is telling the program to load from/ save to an undefined location. Through this method, the program usually reads from/writes to the project folder. Since there's no project folder when published to the web, the program has trouble looking for that specified file. Your best bet would be to add these values to your script.
Here's an example I made:
//starting values
public string ColorName = "Green"
public int Score = 1000;
public float Size =2f;
public int Condition = 1; //We will be using a condition just for this example.
//changing them later
void Update(){ //Update checks for any changes.
if (Condition == 1){ //looks like this because Unity answers doesn't allow the tab key.
ColorName = Green
public int Score = 1000;
public float Size =2f;
}
if (Condition == 2){
ColorName = Red
public int Score = 1000;
public float Size =1.5f;
}
if (Condition == 3){
ColorName = Blue
public int Score = 1000;
public float Size =1f;
}
It may not be what you are going for, but it's the best I can answer this question.