简体   繁体   中英

c# xamlParseException when Deserializing an xml file

Iv Serialized a List and a List to separate xml files using this method.

     private void Save(String filePath,Type saveType)
    {
        // Create a new file stream to write the serialized object to a file
        TextWriter WriteFileStream = new StreamWriter(@filePath);

        // Create a new XmlSerializer instance with the type of List<Journey> and my addition types


        if (saveType == typeof(List<Vechicle>))
        {
            XmlSerializer SerializerObj = new XmlSerializer(saveType);
            //serialising my vechicle list
            SerializerObj.Serialize(WriteFileStream, Vechicle);
        }
        else
        {
            if (saveType == typeof(List<Journey>))
            {
                Type [] extraTypes= new Type[1];
                extraTypes[0] = typeof(Tour);
                XmlSerializer SerializerObj = new XmlSerializer(saveType,extraTypes);
                SerializerObj.Serialize(WriteFileStream, Journey);
            }
        }

        // Cleanup
        WriteFileStream.Close();
    }

this is an example of the vechicls xml file

    <?xml version="1.0" encoding="utf-8"?>
<ArrayOfVechicle xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Vechicle>
<Id>1</Id>
<Registration>a</Registration>
</Vechicle>
<Vechicle>
<Id>2</Id>
<Registration>b</Registration>
</Vechicle>
<Vechicle>
<Id>3</Id>
<Registration>c</Registration>
</Vechicle>
</ArrayOfVechicle>

The problem comes when im trying to load the vechicle date back into my List using this method

    private void Load()
    {

        XmlSerializer SerializerObj = new XmlSerializer(typeof(Vechicle));
        FileStream fs = new FileStream(filepath, FileMode.Open);
        Vechicle = ((List<Vechicle>)SerializerObj.Deserialize(fs));
    }

i get an exception on the final line of the load method. 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll 'The invocation of the constructor on type 'SD2CW2.MainWindow' that matches the specified binding constraints threw an exception.'

You have serialized List<Vechicle> and you should deserialize the same type:

XmlSerializer SerializerObj = new XmlSerializer(typeof(List<Vechicle>));
FileStream fs = new FileStream(filepath, FileMode.Open);
var Vechicles = ((List<Vechicle>)SerializerObj.Deserialize(fs));

You see XamlParseException beacuse serialize Exception occured in place critical for WPF and it wraps serializarion exception in XamlParseException . You could see it in InnerException property of the XamlParseException instance.

But actually in your deserialization code you should handle both types List<Journey> and List<Vechicle> . As you don't save any metadata in your file (which type was serialized), so your code could be like this:

FileStream fs = new FileStream(filepath, FileMode.Open);
List<Vechicle> Vechicles = null;
List<Journey> Journeys = null;

try 
{
    XmlSerializer SerializerObj = new XmlSerializer(typeof(List<Vechicle>));
    Vechicles = ((List<Vechicle>)SerializerObj.Deserialize(fs));
}
catch
{
    Type [] extraTypes= new Type[] { typeof(Tour) };
    XmlSerializer SerializerObj = new XmlSerializer(saveType, extraTypes);
    Journeys = ((List<Journey>)SerializerObj.Deserialize(fs));
}

if ( Vechicles != null)
{
  // do something
} 
else if ( Journeys != null)
{
  // do something
}

Notice that exceptions still can occur here if xmlserializer wouldn't deserialize both types.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM