Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Class
- public class DataItem
- {
- public DataItem(String uniqueId, String title, String link, String description, String image, String date)
- {
- this.UniqueId = uniqueId;
- this.Title = title;
- this.Date = date;
- this.Link = link;
- this.Description = description;
- this.ImagePath = image;
- }
- internal DataItem() { }
- public string UniqueId { get; set; }
- public string Title { get; set; }
- public string Date { get; set; }
- public string Link { get; set; }
- public string ImagePath { get; set; }
- public string Description { get; set; }
- }
- //Save and read methods
- public static async void SaveProgH()
- {
- var folder = ApplicationData.Current.LocalFolder;
- var file = await folder.CreateFileAsync("progh.xml", CreationCollisionOption.ReplaceExisting);
- var subs = await GetGroupAsync("progh");
- Stream fileStream = file.OpenStreamForWriteAsync().Result;
- try
- {
- // serial data object to XML file specified in "DATA_FILE"
- TCD.Serialization.Xml.XmlDeSerializer.SerializeToStream(fileStream, subs.Items);
- fileStream.FlushAsync();
- fileStream.Dispose();
- }
- catch (Exception ex)
- {
- // handle any kind of exceptions
- }
- }
- public static async void GetProgH()
- {
- var folder = ApplicationData.Current.LocalFolder;
- var file = await folder.CreateFileAsync("progh.xml", CreationCollisionOption.OpenIfExists);
- try
- {
- // deserialize the collection object from "DATA_FILE" specified
- var resultado = XmlDeSerializer.DeserializeFromStream(await file.OpenStreamForReadAsync(),
- typeof(List<DataItem>))
- as List<DataItem>;
- var subs = await GetGroupAsync("progh");
- foreach (var item in resultado)
- {
- subs.Items.Add(item);
- }
- }
- catch (Exception ex)
- {
- // handle any kind of exception
- }
- //return null;
- }
- //XML Serializer
- public class XmlDeSerializer
- {
- /// <summary>
- /// Serializes an object and writes it to the Stream.
- /// </summary>
- /// <param name="streamObject">The Stream the object will be written to.</param>
- /// <param name="objForSerialization">The object to serialize.</param>
- public static void SerializeToStream(Stream streamObject, object objForSerialization)
- {
- if (objForSerialization == null || streamObject == null)
- return;
- XmlSerializer serializer = new XmlSerializer(objForSerialization.GetType());
- serializer.Serialize(streamObject, objForSerialization);
- }
- /// <summary>
- /// Deserializes an object from a Stream.
- /// </summary>
- /// <param name="streamObject">The Stream to read from.</param>
- /// <param name="serializedObjectType">The type of the output object.</param>
- /// <returns>New instance of the read object.</returns>
- public static object DeserializeFromStream(Stream streamObject, Type serializedObjectType)
- {
- if (serializedObjectType == null || streamObject == null)
- return null;
- XmlSerializer serializer = new XmlSerializer(serializedObjectType);
- return serializer.Deserialize(streamObject);
- }
- /// <summary>
- /// Serializes an object to a string.
- /// </summary>
- /// <param name="objForSerialization">The object to serialize.</param>
- /// <returns>XML string of the serialized object.</returns>
- public static string SerializeToString(object objForSerialization)
- {
- MemoryStream ms = new MemoryStream();
- XmlSerializer serializer = new XmlSerializer(objForSerialization.GetType());
- serializer.Serialize(ms, objForSerialization);
- ms.Seek(0, SeekOrigin.Begin);
- string serialized = new StreamReader(ms).ReadToEnd();
- ms.Dispose();
- return serialized;
- }
- /// <summary>
- /// Deserializes a string into an object.
- /// </summary>
- /// <param name="serializedObject">The string to deserialize.</param>
- /// <param name="serializedObjectType">The output Type.</param>
- /// <returns>New instance of the read object.</returns>
- public static object DeserializeFromString(string serializedObject, Type serializedObjectType)
- {
- TextReader tr = new StringReader(serializedObject);
- XmlSerializer serializer = new XmlSerializer(serializedObjectType);
- return serializer.Deserialize(tr);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment