public static string Serialize <T>(T message) { XMLOutStream xMLOutStream = new XMLOutStream(); xMLOutStream.Start("object"); SerializeObject(xMLOutStream, typeof(T), message); xMLOutStream.End(); return(xMLOutStream.Serialize()); }
//--------------------------------------------------------------------------------- // static Serialize //--------------------------------------------------------------------------------- public static string Serialize <T>(T message) { #if UNITY_FLASH && !UNITY_EDITOR Debug.LogError("XMLSerializer do not work with Flash !"); return(null); #else XMLOutStream stream = new XMLOutStream(); stream.Start("object"); SerializeObject(stream, typeof(T), message); stream.End(); return(stream.Serialize()); #endif }
void ExampleAddressBook() { // Let's serialize a simple address book XMLOutStream outStream = new XMLOutStream(); outStream.Start("book") .Content("version", 1) .Start("entry") .Content("name", "Mike") .Content("age", 24) .Attribute("already-met", true) .Attribute("married", true) .End() .Start("entry") .Content("name", "John") .Content("age", 32) .Attribute("already-met", false) .End() .End(); string serialized = outStream.Serialize(); // serialized outputs this XML structure: // // // <book> // <entry already-met="true" married=true> // <name>Mike</name> // <age>24</age> // </entry> // <entry already-met="false"> // <name>John</name> // <age>32</age> // </entry> // </book> // // // Deserialize it XMLInStream inStream = new XMLInStream(serialized); // the XML root (here 'book' is automatically entered to parse the content) int version; List <AddressBookEntry> entries = new List <AddressBookEntry>(); inStream.Content("version", out version) .List("entry", delegate(XMLInStream entryStream){ string name; int age; bool alreadyMet; bool married = false; entryStream.Content("name", out name) .Content("age", out age) .Attribute("already-met", out alreadyMet) .AttributeOptional("married", ref married); entries.Add(new AddressBookEntry(name, age, alreadyMet, married)); }); // Now version and entries are set Debug.Log("SERIALIZED XML STRING: " + serialized); string result = ""; foreach (AddressBookEntry entry in entries) { result += entry.ToString() + " "; } Debug.Log("XML DESERIALIZATION of " + entries.Count + " entries: " + result); }
void ExampleAddressBook() { // Let's serialize a simple address book XMLOutStream outStream = new XMLOutStream(); outStream.Start("book") .Content("version", 1) .Start("entry") .Content("name", "Mike") .Content("age", 24) .Attribute("already-met", true) .Attribute("married", true) .End() .Start("entry") .Content("name", "John") .Content("age", 32) .Attribute("already-met", false) .End() .End(); string serialized = outStream.Serialize(); // serialized outputs this XML structure: // // // <book> // <entry already-met="true" married=true> // <name>Mike</name> // <age>24</age> // </entry> // <entry already-met="false"> // <name>John</name> // <age>32</age> // </entry> // </book> // // // Deserialize it XMLInStream inStream = new XMLInStream(serialized); // the XML root (here 'book' is automatically entered to parse the content) int version; List<AddressBookEntry> entries = new List<AddressBookEntry>(); inStream.Content("version", out version) .List("entry", delegate(XMLInStream entryStream){ string name; int age; bool alreadyMet; bool married = false; entryStream.Content("name", out name) .Content("age", out age) .Attribute("already-met", out alreadyMet) .AttributeOptional("married", ref married); entries.Add(new AddressBookEntry(name, age, alreadyMet, married)); }); // Now version and entries are set Debug.Log("SERIALIZED XML STRING: " + serialized); string result = ""; foreach(AddressBookEntry entry in entries) result += entry.ToString() + " "; Debug.Log("XML DESERIALIZATION of " + entries.Count + " entries: " + result); }