using UnityEngine; namespace Crosstales.Common.Util { /// Helper-class for XML. public static class XmlHelper { #if !UNITY_WEBGL || UNITY_EDITOR /// Serialize an object to an 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 { System.IO.File.WriteAllText(filename, SerializeToString(obj)); } catch (System.Exception ex) { Debug.LogError($"Could not serialize the object to a file: {ex}"); } } /// 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 (System.IO.File.Exists(filename)) return DeserializeFromString(System.IO.File.ReadAllText(filename), skipBOM); Debug.LogError($"File doesn't exist: {filename}"); } catch (System.Exception ex) { Debug.LogError($"Could not deserialize the object from a file: {ex}"); } return default; } #endif /// Serialize an object to an XML-string. /// Object to serialize. /// Object as XML-stringValid path public static string SerializeToString(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 System.Text.Encoding.UTF8.GetString(ms.ToArray()); } catch (System.Exception ex) { Debug.LogError($"Could not serialize the object to a string: {ex}"); } return string.Empty; } /// 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 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-2020 crosstales LLC (https://www.crosstales.com)