Ejemplo n.º 1
0
 public static string Serialize <T>(T model) where T : class
 {
     if (null != model)
     {
         try
         {
             var data = new XmlWrapper <T>();
             data.Data = model;
             string xml = "";
             XmlSerializerNamespaces xmlSerializerNamespaces = new XmlSerializerNamespaces();
             xmlSerializerNamespaces.Add("", "");
             XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
             xmlWriterSettings.OmitXmlDeclaration = true;
             XmlSerializer xmlSerializer = new XmlSerializer(typeof(XmlWrapper <T>));
             using (MemoryStream xmlStream = new MemoryStream())
             {
                 using (XmlWriter xmlWriter = XmlWriter.Create(xmlStream, xmlWriterSettings))
                 {
                     xmlSerializer.Serialize(xmlWriter, data, xmlSerializerNamespaces);
                 }
                 xmlStream.Position = 0;
                 StreamReader sr = new StreamReader(xmlStream);
                 xml = sr.ReadToEnd();
                 sr.Dispose();
             }
             return(xml);
         }
         catch
         {
             return(string.Empty);
         }
     }
     else
     {
         return(string.Empty);
     }
 }
Ejemplo n.º 2
0
 public static T DeSerialize <T>(string xml) where T : class, new()
 {
     try
     {
         var model = new XmlWrapper <T>();
         using (StringReader stringReader = new StringReader(xml))
         {
             XmlSerializer xmlSerializer = new XmlSerializer(typeof(XmlWrapper <T>));
             model = (XmlWrapper <T>)xmlSerializer.Deserialize(stringReader);
         }
         if (null != model && null != model.Data)
         {
             return(model.Data);
         }
         else
         {
             return(new T());
         }
     }
     catch
     {
         return(new T());
     }
 }