celsoap

XMLs

Oct 8th, 2014
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.16 KB | None | 0 0
  1. //Class
  2. public class DataItem
  3.     {
  4.         public DataItem(String uniqueId, String title, String link, String description, String image, String date)
  5.         {
  6.             this.UniqueId = uniqueId;
  7.             this.Title = title;
  8.             this.Date = date;
  9.             this.Link = link;
  10.             this.Description = description;
  11.             this.ImagePath = image;
  12.         }
  13.         internal DataItem() { }
  14.        
  15.         public string UniqueId { get; set; }
  16.         public string Title { get; set; }
  17.         public string Date { get; set; }
  18.         public string Link { get; set; }
  19.         public string ImagePath { get; set; }
  20.         public string Description { get; set; }
  21.     }
  22.  
  23. //Save and read methods
  24. public static async void SaveProgH()
  25.         {
  26.             var folder = ApplicationData.Current.LocalFolder;
  27.             var file = await folder.CreateFileAsync("progh.xml", CreationCollisionOption.ReplaceExisting);
  28.  
  29.             var subs = await GetGroupAsync("progh");
  30.  
  31.             Stream fileStream = file.OpenStreamForWriteAsync().Result;
  32.                                        
  33.             try
  34.             {
  35.                 // serial data object to XML file specified in "DATA_FILE"
  36.  
  37.                 TCD.Serialization.Xml.XmlDeSerializer.SerializeToStream(fileStream, subs.Items);
  38.  
  39.                 fileStream.FlushAsync();
  40.                 fileStream.Dispose();
  41.             }
  42.             catch (Exception ex)
  43.             {
  44.                 // handle any kind of exceptions
  45.             }
  46.         }
  47.  
  48.         public static async void GetProgH()
  49.         {
  50.             var folder = ApplicationData.Current.LocalFolder;
  51.  
  52.             var file = await folder.CreateFileAsync("progh.xml", CreationCollisionOption.OpenIfExists);
  53.  
  54.             try
  55.             {
  56.                 // deserialize the collection object from "DATA_FILE" specified
  57.                 var resultado = XmlDeSerializer.DeserializeFromStream(await file.OpenStreamForReadAsync(),
  58.                                                              typeof(List<DataItem>))
  59.                                                              as List<DataItem>;
  60.  
  61.                 var subs = await GetGroupAsync("progh");
  62.  
  63.                 foreach (var item in resultado)
  64.                 {
  65.                     subs.Items.Add(item);
  66.                 }
  67.             }
  68.             catch (Exception ex)
  69.             {
  70.                 // handle any kind of exception
  71.             }
  72.  
  73.             //return null;
  74.         }
  75.  
  76. //XML Serializer
  77. public class XmlDeSerializer
  78.     {
  79.         /// <summary>
  80.         /// Serializes an object and writes it to the Stream.
  81.         /// </summary>
  82.         /// <param name="streamObject">The Stream the object will be written to.</param>
  83.         /// <param name="objForSerialization">The object to serialize.</param>
  84.         public static void SerializeToStream(Stream streamObject, object objForSerialization)
  85.         {
  86.             if (objForSerialization == null || streamObject == null)
  87.                 return;
  88.             XmlSerializer serializer = new XmlSerializer(objForSerialization.GetType());
  89.             serializer.Serialize(streamObject, objForSerialization);
  90.         }
  91.         /// <summary>
  92.         /// Deserializes an object from a Stream.
  93.         /// </summary>
  94.         /// <param name="streamObject">The Stream to read from.</param>
  95.         /// <param name="serializedObjectType">The type of the output object.</param>
  96.         /// <returns>New instance of the read object.</returns>
  97.         public static object DeserializeFromStream(Stream streamObject, Type serializedObjectType)
  98.         {
  99.             if (serializedObjectType == null || streamObject == null)
  100.                 return null;
  101.             XmlSerializer serializer = new XmlSerializer(serializedObjectType);
  102.             return serializer.Deserialize(streamObject);
  103.         }
  104.  
  105.         /// <summary>
  106.         /// Serializes an object to a string.
  107.         /// </summary>
  108.         /// <param name="objForSerialization">The object to serialize.</param>
  109.         /// <returns>XML string of the serialized object.</returns>
  110.         public static string SerializeToString(object objForSerialization)
  111.         {
  112.             MemoryStream ms = new MemoryStream();
  113.             XmlSerializer serializer = new XmlSerializer(objForSerialization.GetType());
  114.             serializer.Serialize(ms, objForSerialization);
  115.             ms.Seek(0, SeekOrigin.Begin);
  116.             string serialized = new StreamReader(ms).ReadToEnd();
  117.             ms.Dispose();
  118.             return serialized;
  119.         }
  120.         /// <summary>
  121.         /// Deserializes a string into an object.
  122.         /// </summary>
  123.         /// <param name="serializedObject">The string to deserialize.</param>
  124.         /// <param name="serializedObjectType">The output Type.</param>
  125.         /// <returns>New instance of the read object.</returns>
  126.         public static object DeserializeFromString(string serializedObject, Type serializedObjectType)
  127.         {
  128.             TextReader tr = new StringReader(serializedObject);
  129.             XmlSerializer serializer = new XmlSerializer(serializedObjectType);
  130.             return serializer.Deserialize(tr);
  131.         }
  132.     }
Advertisement
Add Comment
Please, Sign In to add comment