- Home /
Retrive value from xml
Hi all,
i want to retrive some values from a XML,here is the XML
<?xml version="1.0"?>
<transforms>
<Happy>
<FacePosition>
<x>7</x>
<y>8</y>
<z>9</z>
</FacePosition>
<FaceRotation>
<x>1</x>
<y>2</y>
<z>3</z>
</FaceRotation>
<EyeRotation>
<x>4</x>
<y>5</y>
<z>6</z>
</EyeRotation>
</Happy>
<nervous>
<FacePosition>
<x>1</x>
<y>2</y>
<z>3</z>
</FacePosition>
<FaceRotation>
<x>4</x>
<y>5</y>
<z>6</z>
</FaceRotation>
<EyeRotation>
<x>7</x>
<y>8</y>
<z>9</z>
</EyeRotation>
</nervous>
</transforms>
Here's the method to retrive the values
public static Vector3 LoadFromXml(string elementName,string typename)
{
float X = 0;
float Y = 0;
float Z = 0;
string filepath = Application.dataPath + @"/Resources/Data.xml";
//string filepath = Resources.Load("Data");
XmlDocument xmlDoc = new XmlDocument();
if(File.Exists (filepath))
{
xmlDoc.Load(filepath);
XmlNodeList transformList = xmlDoc.GetElementsByTagName(elementName);
foreach (XmlNode transformInfo in transformList)
{
XmlNodeList transformcontent = transformInfo.ChildNodes;
foreach (XmlNode transformItens in transformcontent)
{
if(transformItens.Name == "x")
{
X = float.Parse(transformItens.InnerText); // convert the strings to float and apply to the Y variable.
}
if(transformItens.Name == "y")
{
Y = float.Parse(transformItens.InnerText); // convert the strings to float and apply to the Y variable.
}
if(transformItens.Name == "z")
{
Z = float.Parse(transformItens.InnerText); // convert the strings to float and apply to the Y variable.
}
}
}
}
return new Vector3(X,Y,Z);;
}
here element name is the name of the state which could be happy or nervous and type name is the name of the sub element facerotation or faceposition or eye rotation.i want to retrive coordinates for a specific state and its subelement.
please help me out i am completely new to XML
wat is the problem error? null ref. or something else?!?! you just posted the code and didn't say way the problem is!
@flamy the problem was i was not able to retrive the specific vallues as i am completely new to xml,i have solved the problem and will post the solution.
Answer by crazyKnight · May 22, 2012 at 06:17 AM
solved myself
heres the solution
public static Vector3 LoadFromXml(string elementName,string typename,int stage)
{
float X = 0;
float Y = 0;
float Z = 0;
string p1 = "x";
string p2 = "y";
string p3 = "z";
string filepath = Application.dataPath + @"/Resources/Data.xml";
//string filepath = Resources.Load("Data");
XmlDocument xmlDoc = new XmlDocument();
if(File.Exists (filepath))
{
xmlDoc.Load(filepath);
XmlNodeList transformList = xmlDoc.GetElementsByTagName(elementName);
foreach (XmlNode transformInfo in transformList)
{
XmlNodeList transformcontent = transformInfo.ChildNodes;
foreach (XmlNode transformItens in transformcontent)
{
if(transformItens.Name == typename)
{
XmlNodeList newtransformList = transformItens.ChildNodes;
foreach (XmlNode newtransformItens in newtransformList)
{
if(newtransformItens.Name == "x")
{
X = float.Parse(newtransformItens.InnerText); // convert the strings to float and apply to the Y variable.
}
if(newtransformItens.Name == "y")
{
Y = float.Parse(newtransformItens.InnerText); // convert the strings to float and apply to the Y variable.
}
if(newtransformItens.Name == "z")
{
Z = float.Parse(newtransformItens.InnerText); // convert the strings to float and apply to the Y variable.
}
}
}
}
}
}
return new Vector3(X,Y,Z);;
}
This is exactly what I'm looking for I can't get it to work though:(
Is this a working example or did you leave something out?
Your answer
Follow this Question
Related Questions
XML parsing c# in unity 1 Answer
Saving serializable class list at runtime. 1 Answer
Easy XML Parsing 4 Answers
Parsing and displaying KML 0 Answers
Problem with XML parse 1 Answer