Created
March 10, 2015 20:39
-
-
Save tiagosr/f82f8f9f5a5444e9ed41 to your computer and use it in GitHub Desktop.
XML Serialization add-ins for C# (esp. Unity)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using System.Xml; | |
using System.Xml.Serialization; | |
using System.IO; | |
using System.Text; | |
using System; | |
// XML-Serializable Dictionary class, | |
// taken from http://stackoverflow.com/questions/12554186/how-to-serialize-deserialize-to-dictionaryint-string-from-custom-xml-not-us | |
[XmlRoot("dictionary")] | |
public class SerializableDictionary<TKey, TValue> | |
: Dictionary<TKey, TValue>, IXmlSerializable | |
{ | |
#region IXmlSerializable Members | |
public System.Xml.Schema.XmlSchema GetSchema() | |
{ | |
return null; | |
} | |
public void ReadXml(System.Xml.XmlReader reader) | |
{ | |
XmlSerializer keySerializer = new XmlSerializer(typeof(TKey)); | |
XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue)); | |
bool wasEmpty = reader.IsEmptyElement; | |
reader.Read(); | |
if (wasEmpty) | |
return; | |
while (reader.NodeType != System.Xml.XmlNodeType.EndElement) | |
{ | |
reader.ReadStartElement("item"); | |
reader.ReadStartElement("key"); | |
TKey key = (TKey)keySerializer.Deserialize(reader); | |
reader.ReadEndElement(); | |
reader.ReadStartElement("value"); | |
TValue value = (TValue)valueSerializer.Deserialize(reader); | |
reader.ReadEndElement(); | |
this.Add(key, value); | |
reader.ReadEndElement(); | |
reader.MoveToContent(); | |
} | |
reader.ReadEndElement(); | |
} | |
public void WriteXml(System.Xml.XmlWriter writer) | |
{ | |
XmlSerializer keySerializer = new XmlSerializer(typeof(TKey)); | |
XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue)); | |
foreach (TKey key in this.Keys) | |
{ | |
writer.WriteStartElement("item"); | |
writer.WriteStartElement("key"); | |
keySerializer.Serialize(writer, key); | |
writer.WriteEndElement(); | |
writer.WriteStartElement("value"); | |
TValue value = this[key]; | |
valueSerializer.Serialize(writer, value); | |
writer.WriteEndElement(); | |
writer.WriteEndElement(); | |
} | |
} | |
#endregion | |
} | |
// XML data | |
public static class XmlFix { | |
public static XmlDocument ReadXml(this string xml) { | |
if(xml != null) { | |
XmlDocument doc = new XmlDocument(); | |
doc.LoadXml(xml); | |
return doc; | |
} | |
return null; | |
} | |
public static string XmlRootName(this Type t) { | |
XmlRootAttribute root = (XmlRootAttribute)Attribute.GetCustomAttribute(t, typeof(XmlRootAttribute)); | |
return root.ElementName; | |
} | |
public static T DeserializeXML<T>(this string xml) where T: class { | |
if(xml != null) { | |
XmlSerializer x = new XmlSerializer(typeof(T)); | |
using (MemoryStream m = new MemoryStream(Encoding.UTF8.GetBytes(xml))) { | |
return (T)x.Deserialize(m); | |
} | |
} | |
return null; | |
} | |
public static T DeserializeXMLDoc<T>(this XmlDocument doc) where T: class { | |
if(doc != null) { | |
XmlSerializer x = new XmlSerializer(typeof(T)); | |
using(XmlNodeReader r = new XmlNodeReader(doc)) { | |
return (T)x.Deserialize(r); | |
} | |
} | |
return null; | |
} | |
public static T DeserializeXMLFile<T>(this string xmlname) where T: class { | |
StreamReader f = File.OpenText(xmlname); | |
if(f!=null) { | |
return f.ReadToEnd().DeserializeXML<T>(); | |
} | |
return null; | |
} | |
public static object DeserializeXML(this string xml, Type tp) { | |
if(xml != null) { | |
XmlSerializer x = new XmlSerializer(tp); | |
using (MemoryStream m = new MemoryStream(Encoding.UTF8.GetBytes(xml))) { | |
return x.Deserialize(m); | |
} | |
} | |
return null; | |
} | |
public static string SerializeXml(this object item) { | |
XmlSerializer s = new XmlSerializer(item.GetType()); | |
using (MemoryStream m = new MemoryStream()) { | |
TextWriter writer = new StreamWriter(m, new System.Text.UTF8Encoding(false)); | |
s.Serialize(writer, item); | |
writer.Flush(); | |
m.Flush(); | |
var encoding = new System.Text.UTF8Encoding(false); | |
// return m.GetBuffer(); | |
return encoding.GetString(m.ToArray()); | |
} | |
} | |
public static void SerializeXmlToFile(this object item, string filename) { | |
StreamWriter w = File.CreateText(filename); | |
w.Write(item.SerializeXml()); | |
w.WriteLine(); | |
w.Close(); | |
} | |
public static T LoadOrCreateFromXML<T>(this string filename, T defaultObject) where T: class { | |
if(File.Exists(filename)) { | |
return filename.DeserializeXMLFile<T>(); | |
} | |
defaultObject.SerializeXmlToFile(filename); | |
return defaultObject; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment