locked
DataContractSerializer IgnoreExtensionDataObject

    Question

  • I'm porting a Windows 7 app to a Windows 8 Store app. In both cases, I'm able to use DataContractSerializer to populate classes by reading in an XML file.

    The problem I'm having is that my list of known types (Data1 through Data5) all inherit from MyBase class that contains an Id property. In the Windows 7 version, the Id is populated as expected. In Windows 8 RT, the Id is NOT being populated.

    The only difference in the code is that the Windows 7 version of DataContractSerializer offer a IgnoreExtensionDataObject parameter, which I set to false (and this works on Windows 7). On WinRT, there's no such parameter to DataContractSerializer.

    My question: Has anyone run into this before, and is there an equivalent or alternative setting available.

    Thanks...

    Windows 7 Code that Works:

    DataContractSerializer serializer =
        new DataContractSerializer(typeof(MyQuery), 
            new List<Type>{typeof(Data1), typeof(Data2), typeof(Data3), 
                            typeof(Data4), typeof(Data5)},
                                    int.MaxValue, false, true /* preserve object refs */, null);
    
    using (FileStream fs = new FileStream(_file, FileMode.Open))
    {
        Model = (MyQuery)serializer.ReadObject(fs);
    }

    Windows RT Code that Works Except that the Base Class Id property not populated:

    List<Type> listOfTypes = new List<Type>
        {
            typeof(Data1), 
            typeof(Data2), 
            typeof(Data3), 
            typeof(Data4), 
            typeof(Data5)
        };
    
    DataContractSerializerSettings dataContractSerializerSettings = new DataContractSerializerSettings();
    dataContractSerializerSettings.MaxItemsInObjectGraph = int.MaxValue;
    dataContractSerializerSettings.PreserveObjectReferences = true;
    dataContractSerializerSettings.KnownTypes = listOfTypes;
    
    DataContractSerializer serializer =
        new DataContractSerializer(typeof(MyQuery), dataContractSerializerSettings);
    
    Stream stream = await storageFile.OpenStreamForReadAsync();
    Model = (MyQuery)serializer.ReadObject(stream);


    Randy

    Thursday, February 20, 2014 2:11 PM

All replies

  • I'll be checking into this - please bump if you don't hear from me by Tuesday.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, February 21, 2014 8:37 PM
    Moderator