예제 #1
0
 public string Serialize <T> (T obj, bool readableOutput = false) where T : class, new()
 {
     try
     {
         System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(obj.GetType(), ExtraTypes.ToArray());
         using (StringWriter writer = new StringWriter())
         {
             XmlWriterSettings settings = (readableOutput ? settingsForReadableOutput : settingsForCompactOutput);
             using (XmlWriter xmlWriter = XmlWriter.Create(writer, settings))
             {
                 xmlSerializer.Serialize(xmlWriter, obj);
                 string xml = writer.ToString();
                 //DebugLog.Info("To XML: " + xml);
                 return(xml);
             }
         }
     }
     catch (Exception e)
     {
         DebugLog.Error("Unable to serialize {0}. XML serialize error: {1}",
                        typeof(T).Name,
                        DebugLog.GetNestedMessages(e));
         throw new Exception("Unable to serialize to XML: " + DebugLog.SafeName(obj), e);
     }
 }
예제 #2
0
 public T Deserialize <T>(string xml) where T : class, new()
 {
     try
     {
         //DebugLog.Info("From XML: " + xml);
         System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
         using (TextReader reader = new StringReader(xml))
         {
             T obj = (T)xmlSerializer.Deserialize(reader);
             return(obj);
         }
     }
     catch (Exception e)
     {
         DebugLog.Error("Unable to deserialize {0}. XML deserialize error: {1}\n{2}",
                        typeof(T).Name,
                        DebugLog.GetNestedMessages(e),
                        xml);
         throw new Exception("Unable to deserialize from XML: " + typeof(T).GetType().Name, e);
     }
 }