简体   繁体   English

反序列化xml文件时c#xamlParseException

[英]c# xamlParseException when Deserializing an xml file

Iv Serialized a List and a List to separate xml files using this method. iv使用此方法序列化一个列表和一个列表以分隔xml文件。

     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 这是vechicls xml文件的示例

    <?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. 我在load方法的最后一行遇到了异常。 '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.' 'PresentationFramework.dll中发生'System.Windows.Markup.XamlParseException''与指定的绑定约束匹配的'SD2CW2.MainWindow'类型的构造函数调用引发了异常。

You have serialized List<Vechicle> and you should deserialize the same type: 您已序列化List<Vechicle>并且应反序列化相同类型:

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 . 您会看到XamlParseException,因为serialize异常发生在对WPF至关重要的位置,并且将serializarion异常包装在XamlParseException You could see it in InnerException property of the XamlParseException instance. 您可以在XamlParseException实例的InnerException属性中看到它。

But actually in your deserialization code you should handle both types List<Journey> and List<Vechicle> . 但是实际上,在反序列化代码中,您应该同时处理List<Journey>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. 请注意,如果xmlserializer不会反序列化这两种类型,则这里仍然会发生异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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