JohnDotOwl
JohnDotOwl

Reputation: 3755

Serialization Exception

I'm trying to store and retrieve data without one application use , means it should refresh instantly on the other list when i redirect , but i'm facing some problem with the saving. would be nice if you could help me out. is the structure alright?

Serialization Exception Type 'System.Collections.Generic.List`1[[Med.sMedication, Med, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' with data contract name 'ArrayOfsMedication:http://schemas.datacontract.org/2004/07/MedReminder_v1' is not expected. 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.

Class File

public class sMedication
{

    public string Name { get; set; }
    public string Remarks { get; set; }
    public string Dosage { get; set; }
    public string Duration { get; set; }
    public DateTime StartDate { get; set; }

    List<string> medicationItem = new List<string> { "", "", "", "", "" };

    public void addtoList()
    {

    }

    public object save(object bigobject)
    {
        List<Object> Obj = new List<Object>();
        Obj.Add(bigobject);

        var settings = IsolatedStorageSettings.ApplicationSettings;
        settings.Add("Obj", Obj);

        settings.Save();
        return true;
    }


}

Adding code

   private void Submit_Clicked(object sender, RoutedEventArgs e)
    {
        sMedication med = new sMedication();
        med.Name = txtName.Text;
        med.Dosage = txtDosage.Text;
        med.Duration = txtDuration.Text;
        med.StartDate = startDate.Value.Value;

        List<sMedication> medicationItem = new List<sMedication> { new sMedication { Name = med.Name, Dosage = med.Dosage } };

        //{ Name, Remarks, Dosage, Duration, Convert.ToString(StartDate) };

        med.save(medicationItem);
        NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
    }

Upvotes: 0

Views: 147

Answers (1)

Matt Lacey
Matt Lacey

Reputation: 65566

ApplicationSettings are serialized using the DataContractSerializer. You can use that yourself to test serializing of your objects. Alternatively you can handle the serialization (and deserialization) yourself and just store stringe in ApplicationSettings.

Your code is also far more complicated than it needs to be. You're trying to save a single sMedication instance but are wrapping that in a List<sMedication> (even though there's only one of them) and then wrapping that in a List<object> again even though it's only a single object.

Upvotes: 1

Related Questions