locked
how to Serialize a Class which have a list<T>,and why i can't use list<T>,and must use IList<T> in winMD

    Question

  • this is not a metro style app ,it is a winMD

    class define:

    public sealed class Book
        {
            public string ID{ get; set; }
            public string Title { get; set; }
            public string Creator { get; set; }
            public string Date { get; set; }
            public string Publisher { get; set; }
            public string Description { get; set; }
            public string Version { get; set; }
            public string Subject { get; set; }
            public string Icon { get; set; }
            public string CreditPoint { get; set; }
            public List<Chapter> Chapters { get; set; }

            public Book()
            {
                Chapters = new List<Chapter>();
            }

        }

    but i get a Exception that the "list" is not a valid windows runtime types, so i use "public IList<Chapter> Chapters { get; set; }" replace "public List<Chapter> Chapters { get; set; }"

    but when i Serialize it :

    public static async void XMLSerialize(StorageFolder folder, string fileName, object instance, Type type)
            {
                XmlSerializer serializer = null;
                try
                {
                    serializer = new XmlSerializer(type);
                }
                catch (Exception ex)
                {
                }
                StorageFile newFile = await FileHelper.CreateFile(folder, fileName);
                Stream newFileStream = await newFile.OpenStreamForWriteAsync();
                serializer.Serialize(newFileStream, instance);
            }

    i get a  Exception "interface 'IList' can't Serialize",what should i do?

    Tuesday, May 08, 2012 5:15 AM

Answers

  • using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices.WindowsRuntime;
    using System.Runtime.Serialization;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml.Serialization;
    using Windows.Foundation;
    using Windows.Storage;

    namespace DataComponent
    {
        public sealed class Serializer
        {
            public async void XMLSerialize(StorageFolder folder, string fileName, object instance, Type type)
            {
                StorageFile newFile = await FileHelper.CreateFile(folder, fileName);
                Stream newFileStream = await newFile.OpenStreamForWriteAsync();
                DataContractSerializer ser = new DataContractSerializer(type);
                ser.WriteObject(newFileStream, instance);
                newFileStream.Dispose();
            }

            public IAsyncOperation<Object> XMLDeserialize(StorageFolder folder, string fileName, Type type)
            {

                // this is Key.  It converts an async task into IAsyncOperation:
                return (IAsyncOperation<Object>)AsyncInfo.Run((System.Threading.CancellationToken ct) => XMLDeserializeTask(folder, fileName, type));
            }

            private async Task<Object> XMLDeserializeTask(StorageFolder folder, string fileName, Type type)
            {
                StorageFile newFile = await FileHelper.OpenFile(folder, fileName);
                Stream newFileStream = await newFile.OpenStreamForReadAsync();
                DataContractSerializer ser = new DataContractSerializer(type);
                Object b = ser.ReadObject(newFileStream);
                newFileStream.Dispose();
                return b;
            }
        }
    }

    using System.IO;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.Text;
    using System.Threading.Tasks;
    using Windows.Data.Xml.Dom;
    using Windows.Storage;

    namespace DataComponent
    {
        [DataContract]
        public sealed class Book
        {
            public string FolderPath { get; set; }
            [DataMember]
            public string ID{ get; set; }
            [DataMember]
            public string Title { get; set; }
            [DataMember]
            public string Creator { get; set; }
            [DataMember]
            public string Date { get; set; }
            [DataMember]
            public string Publisher { get; set; }
            [DataMember]
            public string Description { get; set; }
            [DataMember]
            public string Version { get; set; }
            [DataMember]
            public string Subject { get; set; }
            [DataMember]
            public string Icon { get; set; }
            [DataMember]
            public string CreditPoint { get; set; }
            [DataMember]
            public IList<Chapter> Chapters { get; set; }

            public Book()
            {
                this.Chapters = new List<Chapter>();
            }
        }
    }

    Thursday, May 10, 2012 8:19 AM

All replies

  • I am not familar with your question, maybe you should use data contract for both class Book and Chapters.
    Tuesday, May 08, 2012 7:24 AM
  • I am not familar with your question, maybe you should use data contract for both class Book and Chapters.
    i mean i can't use List<T> to define the Class member,because List<T> is not a valid winrt types , so i use IList<T> ,but when i Serialize the class ,this code "serializer = new XmlSerializer(type);" Exception , interface can't to Serialize.
    Tuesday, May 08, 2012 7:33 AM
  • I don't see your definition for Chapter, what is it?  Is it possible that is the offending part?  Can you test with a list of strings for example to see if that is OK?

    -Jeff


    Jeff Sanders (MSFT)

    Wednesday, May 09, 2012 3:16 PM
    Moderator
  • using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices.WindowsRuntime;
    using System.Runtime.Serialization;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml.Serialization;
    using Windows.Foundation;
    using Windows.Storage;

    namespace DataComponent
    {
        public sealed class Serializer
        {
            public async void XMLSerialize(StorageFolder folder, string fileName, object instance, Type type)
            {
                StorageFile newFile = await FileHelper.CreateFile(folder, fileName);
                Stream newFileStream = await newFile.OpenStreamForWriteAsync();
                DataContractSerializer ser = new DataContractSerializer(type);
                ser.WriteObject(newFileStream, instance);
                newFileStream.Dispose();
            }

            public IAsyncOperation<Object> XMLDeserialize(StorageFolder folder, string fileName, Type type)
            {

                // this is Key.  It converts an async task into IAsyncOperation:
                return (IAsyncOperation<Object>)AsyncInfo.Run((System.Threading.CancellationToken ct) => XMLDeserializeTask(folder, fileName, type));
            }

            private async Task<Object> XMLDeserializeTask(StorageFolder folder, string fileName, Type type)
            {
                StorageFile newFile = await FileHelper.OpenFile(folder, fileName);
                Stream newFileStream = await newFile.OpenStreamForReadAsync();
                DataContractSerializer ser = new DataContractSerializer(type);
                Object b = ser.ReadObject(newFileStream);
                newFileStream.Dispose();
                return b;
            }
        }
    }

    using System.IO;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.Text;
    using System.Threading.Tasks;
    using Windows.Data.Xml.Dom;
    using Windows.Storage;

    namespace DataComponent
    {
        [DataContract]
        public sealed class Book
        {
            public string FolderPath { get; set; }
            [DataMember]
            public string ID{ get; set; }
            [DataMember]
            public string Title { get; set; }
            [DataMember]
            public string Creator { get; set; }
            [DataMember]
            public string Date { get; set; }
            [DataMember]
            public string Publisher { get; set; }
            [DataMember]
            public string Description { get; set; }
            [DataMember]
            public string Version { get; set; }
            [DataMember]
            public string Subject { get; set; }
            [DataMember]
            public string Icon { get; set; }
            [DataMember]
            public string CreditPoint { get; set; }
            [DataMember]
            public IList<Chapter> Chapters { get; set; }

            public Book()
            {
                this.Chapters = new List<Chapter>();
            }
        }
    }

    Thursday, May 10, 2012 8:19 AM