- Home /
C#-XMLSerialize a Struct with Vector3 Array in it
I am rather new to serialization and have it working for some sections but I am running in to an issue.
The first struct:
public struct RecordPt
{
float xP;
float yP;
public RecordPt(float x, float y){
xP = x;
yP = y;
}
}
Works just fine in my serialize code:
public void WriteToXML (string path1,string path2)
{
//Serialize and write first list of RecordPt to xml
XmlSerializer serializer = new XmlSerializer (typeof(List<RecordPt>));
FileStream stream = new FileStream (path, FileMode.Create);
serializer.Serialize (stream, recordingPoints);
stream.Close ();
//Serialize and write second list of LineSeg to xml
XmlSerializer lineSer = new XmlSerializer (typeof(List<LineSeg>));
FileStream lineStream = new FileStream (path2, FileMode.Create);
serializer.Serialize (lineStream, recordingLines);
lineStream.Close ();
}
The second struct has a Vector3 Array that seems to be causing the issue.
public struct LineSeg
{
public Vector3[] pts;
public int lnColor;
public float timeP;
public LineSeg (Vector3[] ptAr, int color, float time)
{
pts = ptAr;
lnColor = color;
timeP = time;
}
}
The error I am getting is:
InvalidOperationException: The type of the argument object 'System.Collections.Generic.List`1[[RecordMovement.SerLines, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]' is not primitive.
Thanks in advance!
~Justin
Answer by tsmitro · Jul 22, 2013 at 04:50 PM
Vector3 is not a primitive and not marked as Serializable. You can wrap it yourself and still provide access to the underlying object (note that I'm using doubles because of the JSON serializer that I use, but that's negligible):
[Serializable]
public class SerializableVector3
{
public double X;
public double Y;
public double Z;
public Vector3 Vector3
{
get
{
return new Vector3((float)X, (float)Y, (float)Z);
}
}
public SerializableVector3() { }
public SerializableVector3(Vector3 vector)
{
double val;
X = double.TryParse(vector.x.ToString(), out val) ? val : 0.0;
Y = double.TryParse(vector.y.ToString(), out val) ? val : 0.0;
Z = double.TryParse(vector.z.ToString(), out val) ? val : 0.0;
}
}
I would advise against using an array, but rather a List of Vector3: see why here. Also, you should wrap your streams in using statements to clean up disposable objects.
Thanks for your help!
I took your advice and made a custom struct for the Vector3's and that seemed to help. Additionally I made an "oh duh" mistake and didn't make the second serialization use the correct XmlSerializer. Copy/paste didn't rename... dough!
Thanks!
~Justin
Nice, I didn't catch the serializer error either, but I'm glad you got it workin.
Answer by nugget70 · Jul 28, 2015 at 05:14 AM
Im trying to serilize a list of vector 3's using your class SerializableVector3
and saving like this:
if(_colliderPos.Count!=0){ //loop through every position in the list for (int i=0; i<_colliderPos.Count;i++){ //create a container for the class SerilizableVector3 and pass collider positiotion to its constructor SerializableVector3 data=new SerializableVector3(_colliderPos[i]); //Write to the file using the data we just sent to the class SerilizableVector3 bf.Serialize(file,data);//write that container to the file } }
but its only saving the first part of the list? what am i doing wrong
p.s. im using binary not XML
Your answer
Follow this Question
Related Questions
Trouble updating old Unity syntax 0 Answers
Best practice to store and load a specific set of objects? 3 Answers
How do I keep references to unique NPCs between scenes? 1 Answer
Serializing an Action as XML? 0 Answers
Multiple Cars not working 1 Answer