BotNavSim  v0.4.3
Mobile Robot Simulation
ObjectSerializer Class Reference

Object serializer utility class. More...

Static Public Member Functions

static void SerializeObject< T > (T serializableObject, string fileName)
 Serializes the object to XML file. More...
 
static T DeSerializeObject< T > (string fileName)
 Deserializes object from XMl file. More...
 
static List< string > SearchForObjects (string path)
 Searchs for XML objects in a directory. More...
 

Detailed Description

Object serializer utility class.

Definition at line 11 of file ObjectSerializer.cs.

Member Function Documentation

static T ObjectSerializer.DeSerializeObject< T > ( string  fileName)
static

Deserializes object from XMl file.

Returns
The deserialized object.
Parameters
fileNameFile name.
Template Parameters
T

Definition at line 47 of file ObjectSerializer.cs.

47  {
48  if (string.IsNullOrEmpty(fileName)) {
49  Debug.LogError("DeSerialize: null or empty filename!");
50  return default(T);
51  }
52 
53  T objectOut = default(T);
54 
55  try {
56  XmlDocument xmlDocument = new XmlDocument();
57  xmlDocument.Load(fileName);
58  string xmlString = xmlDocument.OuterXml;
59 
60  using (StringReader read = new StringReader(xmlString)) {
61  XmlSerializer serializer = new XmlSerializer(typeof(T));
62  using (XmlReader reader = new XmlTextReader(read)) {
63  objectOut = (T)serializer.Deserialize(reader);
64  reader.Close();
65  }
66  read.Close();
67  }
68  }
69  catch(System.Exception e) {
70  Debug.LogException(e);
71  }
72  return objectOut;
73  }
static List<string> ObjectSerializer.SearchForObjects ( string  path)
static

Searchs for XML objects in a directory.

Returns
The for objects.
Parameters
pathPath.

Definition at line 80 of file ObjectSerializer.cs.

80  {
81  List<string> objectsFound = new List<string>();
82  foreach (string file in Directory.GetFiles(path, "*.xml")) {
83  objectsFound.Add(Path.GetFileNameWithoutExtension(file));
84  }
85  return objectsFound;
86  }
static void ObjectSerializer.SerializeObject< T > ( serializableObject,
string  fileName 
)
static

Serializes the object to XML file.

Parameters
serializableObjectSerializable object.
fileNameFile name.
Template Parameters
T

Definition at line 19 of file ObjectSerializer.cs.

19  {
20  if (serializableObject == null) {
21  Debug.LogError("Serialize: cannot serialize a null object!");
22  return;
23  }
24 
25  try {
26  XmlDocument xmlDocument = new XmlDocument();
27  XmlSerializer serializer = new XmlSerializer(serializableObject.GetType());
28  using (MemoryStream stream = new MemoryStream()) {
29  serializer.Serialize(stream, serializableObject);
30  stream.Position = 0;
31  xmlDocument.Load(stream);
32  xmlDocument.Save(fileName);
33  stream.Close();
34  }
35  }
36  catch(System.Exception e) {
37  Debug.LogException(e);
38  }
39  }

The documentation for this class was generated from the following file: