Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8117323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:07:45+00:00 2026-06-06T04:07:45+00:00

I’ve been trying to solve this silly problem for a few days now and

  • 0

I’ve been trying to solve this silly problem for a few days now and it is doing my head in. I would be very happy is somebody has a working example, because the ones I have found so far did not work 🙁
I can serialize basic types, but not objects. And I am very confused in regards to the DataContractAttribute etc. The error I get is:

{“Type ‘SerializeListWinRT.DataModel.LocalStorage+Cat’ with data
contract name
‘LocalStorage.Cat:http://schemas.datacontract.org/2004/07/SerializeListWinRT.DataModel’
is not expected. Consider using a DataContractResolver or add any
types not known statically to the list of known types – for example,
by using the KnownTypeAttribute attribute or by adding them to the
list of known types passed to DataContractSerializer.”}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;
using System.IO;
using System.Runtime.Serialization;
using Windows.Storage.Streams;


namespace SerializeListWinRT.DataModel
{
    class LocalStorage
    {
        [DataContractAttribute]
        public class Cat
        {
            [DataMember()]
            public String Name { get; set; }
        }

        static private Dictionary<string, object> _data = new Dictionary<string, object>();
        private const string filename = "items.xml";

        static public Dictionary<string, object> Data
        {
            get { return _data; }

        }

        static public T GetItem<T>(string key)
        {
            T result = default(T);

            if (_data.ContainsKey(key))
            {
                result = (T)_data[key];
            }

            return result;
        }

        static public bool ContainsItem(string key)
        {
            return _data.ContainsKey(key);
        }

        static async public Task Save()
        {
            await Windows.System.Threading.ThreadPool.RunAsync((sender) =>
            {
                LocalStorage.SaveAsync().Wait();
            }, Windows.System.Threading.WorkItemPriority.Normal);
        }

        static async public Task Restore()
        {
            await Windows.System.Threading.ThreadPool.RunAsync((sender) =>
            {
                LocalStorage.RestoreAsync().Wait();
            }, Windows.System.Threading.WorkItemPriority.Normal);
        }

        static async private Task SaveAsync()
        {
            _data.Add("cat", new Cat { Name = "Myname is" });
            _data.Add("dog", new Cat { Name = "Myname is" });

            StorageFile sessionFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
            IRandomAccessStream sessionRandomAccess = await sessionFile.OpenAsync(FileAccessMode.ReadWrite);
            IOutputStream sessionOutputStream = sessionRandomAccess.GetOutputStreamAt(0);
            DataContractSerializer sessionSerializer = new DataContractSerializer(typeof(Dictionary<string, object>));
            sessionSerializer.WriteObject(sessionOutputStream.AsStreamForWrite(), _data);
            await sessionOutputStream.FlushAsync();
        }

        static async private Task RestoreAsync()
        {
            StorageFile sessionFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists);
            if (sessionFile == null)
            {
                return;
            }
            IInputStream sessionInputStream = await sessionFile.OpenReadAsync();
            DataContractSerializer sessionSerializer = new DataContractSerializer(typeof(Dictionary<string, object>));
            _data = (Dictionary<string, object>)sessionSerializer.ReadObject(sessionInputStream.AsStreamForRead());
        }
    }
}

How I solved the problem:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;
using System.IO;
using System.Runtime.Serialization;
using Windows.Storage.Streams;


namespace SerializeListWinRT.DataModel
{
    class LocalStorage
    {   
        [KnownType(typeof(SerializeListWinRT.Cat))]
        [DataContractAttribute]
        public class Cat
        {
            [DataMember()]
            public String Name { get; set; }
        }


    static private Dictionary<string, object> _data = new Dictionary<string, object>();
    private const string filename = "ngt.xml";

    static public Dictionary<string, object> Data
    {
        get { return _data; }

    }

    static public T GetItem<T>(string key)
    {
        T result = default(T);

        if (_data.ContainsKey(key))
        {
            result = (T)_data[key];
        }

        return result;
    }

    static public bool ContainsItem(string key)
    {
        return _data.ContainsKey(key);
    }

    static async public Task Save<T>()
    {
        await Windows.System.Threading.ThreadPool.RunAsync((sender) =>
        {
            LocalStorage.SaveAsync<T>().Wait();
        }, Windows.System.Threading.WorkItemPriority.Normal);
    }

    static async public Task Restore<T>()
    {
        await Windows.System.Threading.ThreadPool.RunAsync((sender) =>
        {
            LocalStorage.RestoreAsync<T>().Wait();
        }, Windows.System.Threading.WorkItemPriority.Normal);
    }

    static async private Task SaveAsync<T>()
    {
        _data.Add("cat", new Cat { Name = "Myname is" });
        _data.Add("dog", new Cat { Name = "Myname is" });

        StorageFile sessionFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
        IRandomAccessStream sessionRandomAccess = await sessionFile.OpenAsync(FileAccessMode.ReadWrite);
        IOutputStream sessionOutputStream = sessionRandomAccess.GetOutputStreamAt(0);
        DataContractSerializer sessionSerializer = new DataContractSerializer(typeof(Dictionary<string, object>), new Type[] { typeof(T) });
        sessionSerializer.WriteObject(sessionOutputStream.AsStreamForWrite(), _data);
        await sessionOutputStream.FlushAsync();
    }

    static async private Task RestoreAsync<T>()
    {
        StorageFile sessionFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists);
        if (sessionFile == null)
        {
            return;
        }
        IInputStream sessionInputStream = await sessionFile.OpenReadAsync();
        DataContractSerializer sessionSerializer = new DataContractSerializer(typeof(Dictionary<string, object>), new Type[] { typeof(T) });
        _data = (Dictionary<string, object>)sessionSerializer.ReadObject(sessionInputStream.AsStreamForRead());
    }
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T04:07:46+00:00Added an answer on June 6, 2026 at 4:07 am

    I solved this by adding the KnownType attribute

        [KnownType(typeof(SerializeListWinRT.Cat))]
    

    But I havent had to do that before so I was’nt sure if it was a WinRT related problem. Oh well, it does work now,- but I am still curious as to why you have to decorate with the KnownType attribute..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.