Search code examples
c#xmlfilestreamxmlserializer

Debugging a "There was an error generating the XML document" message


I am having a problem with writing a configuration to an XML file when my database is created from a legacy app. The XML document looks as though it is telling me exactly what the problem is but I can't figure out what it means.

My application allows a user to save a configuration to an xml file from a database and I am using an XmlSerializer and a filestream to write my configuration class to an xml document.

This is the write method in the configuration class

    public void Write(Stream stream)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(Configuration));

        serializer.Serialize(stream, this);
    }

This is the statement calling that

            using (FileStream fileStream = new FileStream(filename, FileMode.Create))
            {
                config.Write(fileStream);
            }

This is all working fine and I am able to save the config file. The problem I have though is that we have a psuedo legacy app which is used to set everything up however if this sets the database then everything loads fine in my application but whenever I try to save the config from one of these databases it causes an "There was an error generating the XML document" error message.

I don't really understand how to debug with this. When I look at the generated xml file it just stops mid way through for example

  <ServerIPAddress1>127</ServerIPAddress1>
  <ServerIPAddress2>0</ServerIPAddress2>
  <ServerIPAddress3>0</ServerIPAddress3>
  <ServerIPAddress4>1</Ser

When I saw this I assumed it must mean that the next property was the value giving it a problem however on both versions that value is blank.

Can anyone give me an idea on where to look with this error. I have tried lots of different things such as loading a configuration which was able to be saved to modifying the xml so it is properly formatted and everything seems to work when I save it to a file again it is all working correctly.


Solution

  • Firstly, try to get more informations about the exception: is there an innerException?

    With Visual Studio, you can try to momentarily throw CLR exceptions to get more informations.

    (debug->exceptions->check CLR Exceptions)