簡體   English   中英

反序列化xml文件時出現XamlParseException

[英]XamlParseException when deserializing xml file

我不會問,但我不知道又怎么了。 iv將車輛保存到一個xml文件中,當用戶打開程序時我想反序列化。當我運行它時,我在加載方法的最后一行得到這個

   'System.Windows.Markup.XamlParseException' {"'The invocation of the constructor on type 'SD2CW2.MainWindow' that matches the specified binding constraints threw an exception.'} 

這些是我的加載/保存方法

    private void Load()
    {

        XmlSerializer SerializerObj = new XmlSerializer(typeof(Vechicle));

        // Reading a file requires a FileStream.
        FileStream fs = new FileStream(filepath);
        Vechicle = ((List<Vechicle>)SerializerObj.Deserialize(fs));
    }
    //Save the objects
    private void Save()
    {
        // Create a new file stream to write the serialized object to a file
        TextWriter WriteFileStream = new StreamWriter(filepath);

        Type [] extraTypes= new Type[2];
        extraTypes[0] = typeof(Tour);
        extraTypes[1] = typeof(Vechicle);
        // Create a new XmlSerializer instance with the type of List<Journey> and my addition types
        XmlSerializer SerializerObj = new XmlSerializer(typeof(List<Journey>),extraTypes);

        //serialising my journey list
        SerializerObj.Serialize(WriteFileStream,Journey);
        SerializerObj = new XmlSerializer(typeof(List<Vechicle>));
        //serialising my vechicle list
        SerializerObj.Serialize(WriteFileStream, Vechicle);
        // Cleanup
        WriteFileStream.Close();
    }

這個XML

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfJourney xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" /><?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>1</Registration>
  </Vechicle>
  <Vechicle>
    <Id>2</Id>
    <Registration>2</Registration>
  </Vechicle>
  <Vechicle>
<Id>3</Id>
<Registration>3</Registration>

那是整個XML文件嗎? 因為它不完整。

如果是這樣,請添加

</Vechicle>
</ArrayOfVechicle>
</ArrayOfJourney>

因此,至少您要有一個格式正確的XML文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM