locked
Save selected contacts to file RRS feed

  • Question

  • Hi, can somebody help me here? I'm using the ContactPicker to select some contacts which I want to save the to xml file. I'm doing this.

    public async Task SaveSelectedContacts()
            {
                StorageFolder folder = ApplicationData.Current.LocalFolder;
                IStorageFile contactsFile;
    
                contactsFile = await folder.CreateFileAsync("SavedContacts.xml", CreationCollisionOption.ReplaceExisting);
                using (IRandomAccessStream stream= await contactsFile.OpenAsync(FileAccessMode.ReadWrite))
                    using (Stream outputStream=stream.AsStreamForWrite())
                    {
                        DataContractSerializer serializer=new DataContractSerializer(typeof(ObservableCollection<Contact>));
                        serializer.WriteObject(outputStream, ContactsList);
                    }
            }
    That way I get this exemption "An exception of type 'System.Runtime.Serialization.InvalidDataContractException' occurred in mscorlib.dll but was not handled in user code". So any ideas how can I serialise the contactsFile and save it to a xml file? Thank in advance.

    Monday, April 13, 2015 1:37 PM

Answers

  • It appears the object being serialized is a "Windows.ApplicationModel.Contacts.Contact" object. In order to successfully serialize/deserialize the object using DataContractSerializer, you would need to apply the DataContractAttribute and/or DataMemberAttribute to the class. This means you would have to create a Contact class of your own and apply these missing attributes.

    Hope this helps.


    Abdulwahab Suleiman




    Monday, April 13, 2015 9:32 PM
    Moderator