/// /// © Copyright Sayed Ibrahim Hashimi /// www.sedodream.com /// using System; using System.IO; using System.Xml; using System.Reflection; using System.Runtime.Serialization; using System.Text; using System.Xml.Serialization; namespace Sedodream.Sample.Serialization01.Util { public static class XmlUtils { /// /// Builds an object of the specified type from the given /// XML representation that can be passed to the DataContractSerializer /// public static object BuildFromDataContractXml(string xml, Type type) { if (string.IsNullOrEmpty(xml)) {throw new ArgumentNullException("xml"); } if (type == null) { throw new ArgumentNullException("type"); } object result = null; DataContractSerializer dcs = new DataContractSerializer(type); using (StringReader reader = new StringReader(xml)) using (XmlReader xmlReader = new XmlTextReader(reader)) { result = dcs.ReadObject(xmlReader); } return result; } /// /// Builds an object from its XML /// representation that can be passed to the DataContractSerializer. /// public static T BuildFromDataContractXml(string xml) { if (string.IsNullOrEmpty(xml)) { throw new ArgumentNullException("xml"); } T result = default(T); object objResult = BuildFromDataContractXml(xml, typeof(T)); if (objResult != null) { result = (T)objResult; } return result; } /// /// Gets the XML representation of the given object /// by using the DataContracSerializer. /// public static string GetDataContractXml(T obj) { if (obj == null) { throw new ArgumentNullException("obj"); } return GetDataContractXml(obj.GetType(), obj); } /// /// Gets the XML representation of the given object /// of specfiied type by using the DataContracSerializer. /// public static string GetDataContractXml(Type type, object val) { if (type == null) { throw new ArgumentNullException("type"); } if (val == null) { throw new ArgumentNullException("val"); } MemoryStream ms = new MemoryStream(); string xml = null; try { DataContractSerializer dcs = new DataContractSerializer(type); using (XmlTextWriter xmlTextWriter = new XmlTextWriter(ms, System.Text.Encoding.Default)) { xmlTextWriter.Formatting = System.Xml.Formatting.Indented; dcs.WriteObject(xmlTextWriter, val); xmlTextWriter.Flush(); ms = (MemoryStream)xmlTextWriter.BaseStream; ms.Flush(); xml = UTF8ByteArrayToString(ms.ToArray()); } } finally { if (ms != null) { ms.Close(); ms = null; } } return xml; } /// /// Writes the XML representation from the DataContractSerializer /// into the specified filename. If a file at filename /// already exists then an Exception will be thrown. /// public static void WriteDateContractToFile(string filename, T obj) { WriteDateContractToFile(filename, obj.GetType(), obj); } /// /// Writes the XML representation from the DataContractSerializer /// into the specified filename. If a file at filename /// already exists then an Exception will be thrown. /// public static void WriteDateContractToFile(string filename, Type type, object val) { if (string.IsNullOrEmpty(filename)) { throw new ArgumentNullException("filename"); } if (val == null) { throw new ArgumentNullException("val"); } if (File.Exists(filename)) { throw new ArgumentException("filname"); } //TODO: Stream this into the file instead of this!!! File.WriteAllText(filename, GetDataContractXml(type, val)); } private static String UTF8ByteArrayToString(Byte[] characters) { UTF8Encoding encoding = new UTF8Encoding(); string constructedString = encoding.GetString(characters); return (constructedString); } private static Byte[] StringToUTF8ByteArray(String pXmlString) { UTF8Encoding encoding = new UTF8Encoding(); byte[] byteArray = encoding.GetBytes(pXmlString); return byteArray; } } }