- Home /
Parse XML to Vector3 (and other objects you don't control)
Hello,
I am attempting to parse XML into a Vector3 using the least amount of code possible. Since Vector3 is not a Serializable class, there appears to be no way to populate a Vector3 from XML "automatically".
Basically, I pass the attribute directly into a property named Position_Surrogate
. This property then uses a Vector3 extension method to populate the real Vector3 object (named position
).
I am wondering if anyone out there has a better or more elegant solution.
The XML
<?xml version="1.0" encoding="utf-8"?>
<Test Name="Test" Position="1.0, 2.0, 3.0">
</Test>
The Vector3
public Vector3 position;
[XmlAttribute("Position")]
public string Position_Surrogate{
get{ return ""; }
set{
position = new Vector3().FromString(value);
}
}
The Vector3 Extension Method
public static class Vector3Helper
{
public static Vector3 FromString(this Vector3 vector, string value){
string[] temp = value.Replace(" ", "").Split(',');
vector.x = float.Parse(temp[0]);
vector.y = float.Parse(temp[1]);
vector.z = float.Parse(temp[2]);
return vector;
}
}
Your answer
Follow this Question
Related Questions
"Root Element Missing: XML Exception" when trying to load data from XML file 2 Answers
Serialize a class that extends as a serializable class? 0 Answers
XML Serialization issues 0 Answers
loading XML quiz 1 Answer
XML Serialization not working on WebGL 0 Answers