locked
How do I store any type of object in Windows 8.1 RRS feed

  • Question

  • how do I store any kind of Object? I have custom classes that I cannot figure out how to store using "Windows.Storage.ApplicationData.Current.LocalSettings".

    In Windows Phone 8 I use the following to save the object:

    //Storage
    IsolatedStorageSettings storage = IsolatedStorageSettings.ApplicationSettings;
    
    //Custom Object CLass
    portraitsFeed feed = new portraitsFeed();
    
    //Check if IsoStorage has object
    if (storage.Contains("feed"))
    {
        //Remove Old Object
        storage.Remove("feed");
    }
    
    //Save New Cusotm Object
    storage.Add("feed", feed);
    storage.Save();
    
    //Read Object
    portraitsFeed newFeed = (portraitsFeed)storage["feed"];

    How do I do the same in Windows 8.1? THANKS!


    Bobby Harrell

    Thursday, November 21, 2013 7:58 PM

Answers

  • This is a piece of my code

    The class (without attributes [DataMember()])

    public class New
        {
            public string title { get; set; }
            public string description { get; set; }
            public Uri url { get; set; }
        }

    I save several news when a handler is called after one method download they from internet so e.News are all the news in an ObservableCollection

     NewsList = new ObservableCollection<New>(e.News);
                    var FileName = "News.xml";
                    DataContractSerializer serializer = new DataContractSerializer(typeof(ObservableCollection<New>));
    
                    var localFolder = ApplicationData.Current.LocalFolder;
                    var file = await localFolder.CreateFileAsync(FileName,CreationCollisionOption.ReplaceExisting);
                    IRandomAccessStream sessionRandomAccess = await file.OpenAsync(FileAccessMode.ReadWrite);
                    IOutputStream sessionOutputStream = sessionRandomAccess.GetOutputStreamAt(0);
                    serializer.WriteObject(sessionOutputStream.AsStreamForWrite(), NewsList);


    The read method

    var FileNameNews = "News.xml";
                    DataContractSerializer serializer = new DataContractSerializer(typeof(ObservableCollection<New>));
                    var localFolder = ApplicationData.Current.LocalFolder;
    
                    var newsFile = await localFolder.GetFileAsync(FileNameNews);
    
                    IInputStream sessionInputStream = await newsFile.OpenReadAsync();
                    newsVM = new NewsViewModel();
                    NewsVM.NewsList = (ObservableCollection<New>)serializer.ReadObject(sessionInputStream.AsStreamForRead());

    NewsVM.NewsList is an ObservableCollection of New class I Databind it with a ListBox.

    I save the news in a file not in the settings but the serialization is the same.

    • Marked as answer by Anne Jing Thursday, November 28, 2013 7:01 AM
    Friday, November 22, 2013 4:07 AM

All replies

  • I found this: Blog 

    But it gives the error: There was an error reflecting type 'MadeMistakes.portraitsFeed'.

    If I keep digging down through the exception, multiple InnerExceptions, it goes through my custom class and gives me this at the bottom InnerException: System.Uri cannot be serialized because it does not have a parameterless constructor.

    How can I make my class Serializable in a way that it will retain all aspects of the object, like it did with the Windows Phone code above.


    Thanks, Bobby Harrell

    Thursday, November 21, 2013 9:32 PM
  • I have been digging through the Storage Classes, I think the problem is my classes are not serializable and I dont know how to make them as such :-), please see my other replies as well.

    Thanks, Bobby Harrell

    Thursday, November 21, 2013 9:47 PM
    • Edited by HJR046 Friday, November 22, 2013 12:03 AM Add more information
    Friday, November 22, 2013 12:00 AM
  • Thanks for the suggestion, I eagerly added the code for DataContactSerializer but some of the parts apparently are confused, Windows 8.1 project does not know what a FileStream or IExtensibleDataObject is, I do have all of what I thought were the necessary usings. if I click them the intellisense does not recognise them to add anymore usings.

    Below is the entire thing.

    Suggestions?

    Thanks for all your help thus far!

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Runtime.Serialization;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Threading.Tasks;
    using System.Xml;
    using System.Xml.Linq;
    using Windows.Web.Syndication;
    
    namespace MadeMistakes
    {
        public class RssFeedDO
        {
            public string RssFeedUrl { get; private set; }
    
            public RssFeedDO()
            {
                this.RssFeedUrl = "urlOfRSSFeed";
            }
    
            public List<Uri> FetchLinksFromSource(string htmlSource)
            {
                List<Uri> links = new List<Uri>();
                string regexImgSrc = @"<img[^>]*?src\s*=\s*[""']?([^'"" >]+?)[ '""][^>]*?>";
                string regexOtherLinks = @"<a[^>]*?href\s*=\s*[""']?([^'"" >]+?)[ '""][^>]*?>";
                MatchCollection matchesImgSrc = Regex.Matches(htmlSource, regexImgSrc, RegexOptions.IgnoreCase | RegexOptions.Singleline);
                MatchCollection matchesOtherLinks = Regex.Matches(htmlSource, regexOtherLinks, RegexOptions.IgnoreCase | RegexOptions.Singleline);
                foreach (Match m in matchesImgSrc)
                {
                    string href = m.Groups[1].Value;
                    links.Add(new Uri(href));
                }
                foreach (Match l in matchesOtherLinks)
                {
                    string href = l.Groups[1].Value;
                    links.Add(new Uri(href));
                }
                return links;
            }
    
            public static void WriteObject(portraitsFeed feed)
            {
                FileStream writer = new FileStream("savedFeed.xml", FileMode.Create);
                DataContractSerializer ser = new DataContractSerializer(typeof(portraitsFeed));
                ser.WriteObject(writer, feed);
                writer.Close();
            }
    
            public static portraitsFeed ReadObject()
            {
                FileStream fs = new FileStream("savedFeed.xml", FileMode.Open);
                XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas());
                DataContractSerializer ser = new DataContractSerializer(typeof(portraitsFeed));
    
                // Deserialize the data and read it from the instance.
                portraitsFeed feed = (portraitsFeed)ser.ReadObject(reader, true);
                //reader.Close();
                fs.Close();
    
                return feed;
            }
        }
    
        [DataContract(Name = "portraitsFeed", Namespace = "MadeMistakes")]
        public class portraitsFeed : IExtensibleDataObject
        {
            [DataMember()]
            public string title { get; set; }
            [DataMember()]
            public string subtitle { get; set; }
            [DataMember()]
            public string icon { get; set; }
            [DataMember()]
            public Author author { get; set; }
            [DataMember()]
            public string rights { get; set; }
            [DataMember()]
            public List<Entry> portraits { get; set; }
            [DataMember()]
            public List<Entry> others { get; set; }
        }
    
        [DataContract(Name = "Author", Namespace = "MadeMistakes")]
        public class Author : IExtensibleDataObject
        {
            [DataMember()]
            public string name { get; set; }
            [DataMember()]
            public string url { get; set; }
            [DataMember()]
            public string email { get; set; }
        }
    
        [DataContract(Name = "Entry", Namespace = "MadeMistakes")]
        public class Entry : IExtensibleDataObject
        {
            [DataMember()]
            public string title { get; set; }
            [DataMember()]
            public string published { get; set; }
            [DataMember()]
            public Author author { get; set; }
            [DataMember()]
            public Content content { get; set; }
            [DataMember()]
            public string webLink { get; set; }
        }
    
        [DataContract(Name = "Content", Namespace = "MadeMistakes")]
        public class Content : IExtensibleDataObject
        {
            [DataMember()]
            public string bulk { get; set; }
            [DataMember()]
            public Uri portraitSmall { get; set; }
            [DataMember()]
            public Uri portraitLarge { get; set; }
            [DataMember()]
            public Uri twitter { get; set; }
            [DataMember()]
            public string twitterHandle { get; set; }
            [DataMember()]
            public About about { get; set; }
            [DataMember()]
            public List<Uri> screenshots { get; set; }
            [DataMember()]
            public string screenshotscaption { get; set; }
            [DataMember()]
            public List<Uri> otherLinks { get; set; }
            [DataMember()]
            public int index { get; set; }
            
        }
    
        [DataContract(Name = "About", Namespace = "MadeMistakes")]
        public class About : IExtensibleDataObject
        {
            [DataMember()]
            public string twitter { get; set; }
            [DataMember()]
            public string stylus { get; set; }
            [DataMember()]
            public string application { get; set; }
            [DataMember()]
            public string info { get; set; }
        }
    }
    


    Thanks, Bobby Harrell

    Friday, November 22, 2013 3:26 AM
  • This is a piece of my code

    The class (without attributes [DataMember()])

    public class New
        {
            public string title { get; set; }
            public string description { get; set; }
            public Uri url { get; set; }
        }

    I save several news when a handler is called after one method download they from internet so e.News are all the news in an ObservableCollection

     NewsList = new ObservableCollection<New>(e.News);
                    var FileName = "News.xml";
                    DataContractSerializer serializer = new DataContractSerializer(typeof(ObservableCollection<New>));
    
                    var localFolder = ApplicationData.Current.LocalFolder;
                    var file = await localFolder.CreateFileAsync(FileName,CreationCollisionOption.ReplaceExisting);
                    IRandomAccessStream sessionRandomAccess = await file.OpenAsync(FileAccessMode.ReadWrite);
                    IOutputStream sessionOutputStream = sessionRandomAccess.GetOutputStreamAt(0);
                    serializer.WriteObject(sessionOutputStream.AsStreamForWrite(), NewsList);


    The read method

    var FileNameNews = "News.xml";
                    DataContractSerializer serializer = new DataContractSerializer(typeof(ObservableCollection<New>));
                    var localFolder = ApplicationData.Current.LocalFolder;
    
                    var newsFile = await localFolder.GetFileAsync(FileNameNews);
    
                    IInputStream sessionInputStream = await newsFile.OpenReadAsync();
                    newsVM = new NewsViewModel();
                    NewsVM.NewsList = (ObservableCollection<New>)serializer.ReadObject(sessionInputStream.AsStreamForRead());

    NewsVM.NewsList is an ObservableCollection of New class I Databind it with a ListBox.

    I save the news in a file not in the settings but the serialization is the same.

    • Marked as answer by Anne Jing Thursday, November 28, 2013 7:01 AM
    Friday, November 22, 2013 4:07 AM