0

I wouldnt ask but i have no idea whats wrong again. Iv saved vehicles to an xml file and when the user opens the program i want to deserialize.when i run it i get this on the final line in the load method

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

these are my load/save methods

    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();
    }

this 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>

3
  • Why are you serializing two different objects (Journey, Vehicle) to the same file? Only one of these Serialize operations is followed by a flush to disk; this may be the cause of the missing tags mentioned in the @splrs answer. Commented Dec 10, 2013 at 21:48
  • @groverboy iv changed it so it serialize's to two xml files the tags are now correct but im still unable to load. Commented Dec 11, 2013 at 0:31
  • If the tags were incorrect as mentioned in the @splrs answer then you should accept that answer. If you are still unable to load, create a new question and post your updated code there. Commented Dec 11, 2013 at 0:36

1 Answer 1

1

Is that the entire XML file? Because it's incomplete.

If so, add

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

So you've at least got a well-formed XML document to start with.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.