Hi all,
I’m currently creating a simple multiplayer game using an authorative server approach (more of a learning curve for me than anything). I’ve hit a snag when it comes to passing a complex game object structure from the client to the server.
I’m using XML Serialiser to send the xml structure over, and the structure is getting there in one piece and I can see from the debug log its held all its elements etc.
My problem comes when I try to deserialise this into its original class. example class structure:
[XmlRoot("Armies")]
[System.Serializable]
public class Armies {
public Army army = new Army();
public string armyName;
[System.Serializable]
public class Army {
[XmlArray("regiments")]
[XmlArrayItem("Regiment")]
public List<Regiment> regiments = new List<Regiment>();
[System.Serializable]
public class Regiment
{
public string regimentName;
}
}
}
It goes through and gets the right fields for the first part (i.e. army section) but once it comes to the list of regiments it leaves it blank (empty), and just returns a collection of regiments of 0.
From what I’ve read this is because it doesn’t understand the type as it is serialised/deserialised so doesn’t understand how to recreate the sub class??
Would be very appreciative of any advice/guidance on:
a) a better way to pass data structures for a multiplayer game, or
b) if there is something i’m fundamentally doing wrong that could fix this issue? I’ve tried researching this but haven’t been able to come to any solutions!
I’ve included my serialiser code for reference:
public string SerializeXMLObject(Type myType, object pObject)
{
string XmlizedString = null;
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(myType);
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xs.Serialize(xmlTextWriter, pObject);
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
return XmlizedString;
}
// Here we deserialize it back into its original form
public object DeserializeObject(Type myType, string pXmlizedString)
{
XmlSerializer xs = new XmlSerializer(myType);
MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
return xs.Deserialize(memoryStream);
}