using System; using System.IO; using System.Xml.Linq; using System.Xml.Serialization; using JD.Core.Logging; namespace JD.Core { public static class XmlExtensions { public static bool Deserialize(string filePath, out T destObject, Type expectedType = null) where T : class { expectedType = expectedType ?? typeof (T); try { var element = XElement.Load(filePath); var serializer = new XmlSerializer(expectedType); destObject = (T)serializer.Deserialize(element.CreateReader()); return true; } catch (Exception ex) { var log = LogExtensions.GetLog(); if (log != null) { log.Error(LogCategories.Get("Xml"), "Failed to deserialize file \"{0}\" to an object of type {1}. ", filePath, typeof(T).Name); log.Exception(ex); } destObject = null; return false; } } public static bool Deserialize(Stream stream, out T destObject) where T : class { try { var element = XElement.Load(stream); var serializer = new XmlSerializer(typeof(T)); destObject = (T)serializer.Deserialize(element.CreateReader()); return true; } catch (Exception ex) { var log = LogExtensions.GetLog(); if (log != null) { log.Error(LogCategories.Get("Xml"), "Failed to deserialize a stream to an object of type {0}. ", typeof(T).Name); log.Exception(ex); } destObject = default(T); return false; } } public static XElement ToXml(this T objectToSerialize) { try { using (var memoryStream = new MemoryStream()) { using (TextWriter streamWriter = new StreamWriter(memoryStream, System.Text.Encoding.Default)) { var serializer = new XmlSerializer(objectToSerialize.GetType()); serializer.Serialize(streamWriter, objectToSerialize); return XElement.Parse(System.Text.Encoding.Default.GetString(memoryStream.ToArray())); } } } catch (Exception ex) { var log = LogExtensions.GetLog(); if (log != null) { log.Error(LogCategories.Get("Xml"), "Failed to convert an object of type {0} to xml. ", typeof(T).Name); log.Exception(ex); } return null; } } public static bool Serialize(this T objectToSerialize, string filePath) { try { XElement element = objectToSerialize.ToXml(); if (element == null) return false; using (TextWriter tw = new StreamWriter(filePath)) { element.Save(tw); return true; } } catch (Exception ex) { var log = LogExtensions.GetLog(); if (log != null) { log.Error(LogCategories.Get("Xml"), "Failed to serialize an object of type {0} to the file \"{1}\". ", typeof(T).Name, filePath); log.Exception(ex); } return false; } } public static bool Serialize(this T objectToSerialize, Stream destStream) { try { XElement element = objectToSerialize.ToXml(); if (element == null) return false; using (TextWriter tw = new StreamWriter(destStream)) { element.Save(tw); return true; } } catch (Exception ex) { var log = LogExtensions.GetLog(); if (log != null) { log.Error(LogCategories.Get("Xml"), "Failed to serialize an object of type {0} to a stream. ", typeof(T).Name); log.Exception(ex); } return false; } } public static string GetAttributeValue(this XElement element, XName name, string defaultValue = null) { var attribute = element.Attribute(name); if (attribute == null) return defaultValue; else return attribute.Value; } } }