locked
Problem loading xml file with DataContractSerializer RRS feed

  • Question

  • I can't figure out why the following code just hangs with no error.

             public async Task<T> LoadASync()
            {
                try
                {
                    StorageFile file = null;
                    StorageFolder folder = GetFolder(storageType);
                    file = await folder.GetFileAsync(FileName(Activator.CreateInstance<T>()));
                    IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read);

                    Stream stream = readStream.OpenRead(0);
                    DataContractSerializer xs = new DataContractSerializer(typeof(T));
           
                    using (XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(stream, XmlDictionaryReaderQuotas.Max))
                    {
                        T obj = (T)xs.ReadObject(reader, true);
                        return (T)obj;
                    }
                }
                catch (FileNotFoundException)
                {
                    //file not existing is perfectly valid so simply return the default
                    return default(T);
                }
                catch (Exception e)
                {
                    var errMsg = e.Message;
                    //Unable to load contents of file
                    throw;
                }
            }

    All seems to run fine until it hits the using statement ( using (XmlDictionaryReader reader... ) then it just hangs with no errors.

    Any clues?

    Thx,

    Andy

    Wednesday, December 28, 2011 5:39 AM

Answers

  • FYI,

    I took a different approach based on further research.  I ended up resolving as follows:

                try
                {
                    StorageFile file = null;
                    StorageFolder folder = GetFolder(storageType);
                    file = await folder.GetFileAsync(FileName(fileName));

     

                    var stream = await file.OpenForReadAsync();
                    var data = new byte[file.Size];
                    await stream.ReadAsync(data.AsBuffer(), (uint)data.Length, InputStreamOptions.None);
                    var reader = new StreamReader(new MemoryStream(data));

                    var result = await Task<string>.Run(() => reader.ReadToEnd());
                    DataContractSerializer xs = new DataContractSerializer(typeof(T));
                    Encoding e2 = Encoding.GetEncoding("utf-16");
                    MemoryStream memoryStream = new MemoryStream(Encoding.Unicode.GetBytes(result));
                    memoryStream.Position = 0;               

                    using (XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(memoryStream, XmlDictionaryReaderQuotas.Max))
                    {
                        T obj = (T)xs.ReadObject(reader, true);
                        return (T)obj;
                    }
                }

                catch (FileNotFoundException)
                {
                    //file not existing is perfectly valid so simply return the default
                    return default(T);
                }

                catch (Exception e)
                {
                    var errMsg = e.Message;
                    //Unable to load contents of file
                    throw;
                }


    Andy Espinosa, Social Bend, LLC.
    • Marked as answer by Andy Espinosa Friday, January 6, 2012 2:33 PM
    Tuesday, January 3, 2012 3:02 PM

All replies

  • So this is not a problem with DataContractSerializer but a problem with XmlDictionaryReader.CreateTextReader?

     


    Jeff Sanders (MSFT)
    Wednesday, December 28, 2011 3:23 PM
    Moderator
  • FYI,

    I took a different approach based on further research.  I ended up resolving as follows:

                try
                {
                    StorageFile file = null;
                    StorageFolder folder = GetFolder(storageType);
                    file = await folder.GetFileAsync(FileName(fileName));

     

                    var stream = await file.OpenForReadAsync();
                    var data = new byte[file.Size];
                    await stream.ReadAsync(data.AsBuffer(), (uint)data.Length, InputStreamOptions.None);
                    var reader = new StreamReader(new MemoryStream(data));

                    var result = await Task<string>.Run(() => reader.ReadToEnd());
                    DataContractSerializer xs = new DataContractSerializer(typeof(T));
                    Encoding e2 = Encoding.GetEncoding("utf-16");
                    MemoryStream memoryStream = new MemoryStream(Encoding.Unicode.GetBytes(result));
                    memoryStream.Position = 0;               

                    using (XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(memoryStream, XmlDictionaryReaderQuotas.Max))
                    {
                        T obj = (T)xs.ReadObject(reader, true);
                        return (T)obj;
                    }
                }

                catch (FileNotFoundException)
                {
                    //file not existing is perfectly valid so simply return the default
                    return default(T);
                }

                catch (Exception e)
                {
                    var errMsg = e.Message;
                    //Unable to load contents of file
                    throw;
                }


    Andy Espinosa, Social Bend, LLC.
    • Marked as answer by Andy Espinosa Friday, January 6, 2012 2:33 PM
    Tuesday, January 3, 2012 3:02 PM