using UnityEngine;
namespace Crosstales.Common.Util
{
/// Helper-class for XML.
public abstract class XmlHelper
{
/// Serialize an object to a XML-file.
/// Object to serialize.
/// File name of the XML.
public static void SerializeToFile(T obj, string filename)
{
if (null == obj)
throw new System.ArgumentNullException(nameof(obj));
if (filename == null)
throw new System.ArgumentNullException(nameof(filename));
try
{
FileHelper.WriteAllText(filename, SerializeToString(obj));
}
catch (System.Exception ex)
{
Debug.LogError($"Could not serialize the object to a file: {ex}");
}
}
/// Serialize an object to a XML-string.
/// Object to serialize.
/// Object as XML-string
public static string SerializeToString(T obj)
{
if (null == obj)
throw new System.ArgumentNullException(nameof(obj));
byte[] result = SerializeToByteArray(obj);
return result != null ? System.Text.Encoding.UTF8.GetString(result).Trim('\uFEFF', '\u200B') : string.Empty; //remove invalid BOM
}
/// Serialize an object to a XML byte-array.
/// Object to serialize.
/// Object as byte-array
public static byte[] SerializeToByteArray(T obj)
{
if (null == obj)
throw new System.ArgumentNullException(nameof(obj));
try
{
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(obj.GetType());
System.Xml.XmlTextWriter xmlTextWriter = new System.Xml.XmlTextWriter(ms, System.Text.Encoding.UTF8);
xs.Serialize(xmlTextWriter, obj);
ms = (System.IO.MemoryStream)xmlTextWriter.BaseStream;
return ms.ToArray();
}
catch (System.Exception ex)
{
Debug.LogError($"Could not serialize the object to a byte-array: {ex}");
}
return null;
}
/// Deserialize a XML-file to an object.
/// XML-file of the object
/// Skip BOM (optional, default: false)
/// Object
public static T DeserializeFromFile(string filename, bool skipBOM = false)
{
if (filename == null)
throw new System.ArgumentNullException(nameof(filename));
try
{
if (FileHelper.ExistsFile(filename))
{
string data = FileHelper.ReadAllText(filename);
if (string.IsNullOrEmpty(data))
{
Debug.LogWarning($"Data was null: {filename}");
}
else
{
return DeserializeFromString(data, skipBOM);
}
}
else
{
Debug.LogError($"File does not exist: {filename}");
}
}
catch (System.Exception ex)
{
Debug.LogError($"Could not deserialize the object from a file: {ex}");
}
return default;
}
/// Deserialize a XML-string to an object.
/// XML of the object
/// Skip BOM (optional, default: true)
/// Object
public static T DeserializeFromString(string xmlAsString, bool skipBOM = true)
{
if (string.IsNullOrEmpty(xmlAsString))
throw new System.ArgumentNullException(nameof(xmlAsString));
try
{
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
using (System.IO.StringReader sr = new System.IO.StringReader(xmlAsString.Trim()))
{
if (skipBOM)
sr.Read(); //skip BOM
return (T)xs.Deserialize(sr);
}
}
catch (System.Exception ex)
{
Debug.LogError($"Could not deserialize the object from a string: {ex}");
}
return default;
}
/// Deserialize a XML byte-array to an object.
/// XML of the object
/// Object
public static T DeserializeFromByteArray(byte[] data)
{
if (data == null)
throw new System.ArgumentNullException(nameof(data));
try
{
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
System.IO.MemoryStream ms = new System.IO.MemoryStream(data);
return (T)xs.Deserialize(ms);
}
catch (System.Exception ex)
{
Debug.LogError($"Could not deserialize the object from a byte-array: {ex}");
}
return default;
}
/// Deserialize a Unity XML resource (TextAsset) to an object.
/// Name of the resource
/// Skip BOM (optional, default: true)
/// Object
public static T DeserializeFromResource(string resourceName, bool skipBOM = true)
{
if (string.IsNullOrEmpty(resourceName))
throw new System.ArgumentNullException(nameof(resourceName));
// Load the resource
TextAsset xml = Resources.Load(resourceName) as TextAsset;
return xml != null ? DeserializeFromString(xml.text, skipBOM) : default;
}
}
}
// © 2014-2023 crosstales LLC (https://www.crosstales.com)