How to debug XMLSerializer.Deserialize error : 'There is an error in XML document.' FormatException: Input string was not in a correct format.

Richard Scannell 321 Reputation points
2021-10-21T11:20:40.317+00:00

I need to find out which XML node is triggering an XMLSerializer.Deserialize error System.InvalidOperationException: 'There is an error in XML document (6, 3135).'
FormatException: Input string was not in a correct format.

This exception was originally thrown at this call stack:
System.Number.StringToNumber(string, System.Globalization.NumberStyles, ref System.Number.NumberBuffer, System.Globalization.NumberFormatInfo, bool)
System.Number.ParseInt32(string, System.Globalization.NumberStyles, System.Globalization.NumberFormatInfo)
byte.Parse(string, System.Globalization.NumberStyles, System.Globalization.NumberFormatInfo)
<Unknown Method>
<Unknown Method>
<Unknown Method>

This is the code

        var document10 = new XmlDocument();
        document10.Load(“C:/Users/Me/file.xml”);

        XmlSerializer serializer10 = new XmlSerializer(typeof(Form10.Template), new XmlRootAttribute
        {
            ElementName = "Template",
            Namespace = http://www.url/folder/form
        });

        serializer10.UnknownAttribute += new XmlAttributeEventHandler(Serializer_UnknownAttribute);
        serializer10.UnknownNode += new XmlNodeEventHandler(Serializer_UnknownNode);
        serializer10.UnknownElement += new XmlElementEventHandler(Serializer_Element);
        serializer10.UnreferencedObject += new UnreferencedObjectEventHandler(Serializer_UnreferencedObject);

        TextReader sr10 = new StringReader(document10.InnerXml);
        Form10.Template filedata10 = (Form10.Template)serializer.Deserialize(sr10);
    }


    public static void Serializer_UnknownAttribute(object sender, XmlAttributeEventArgs e)
    {
        Console.WriteLine("Unknown Attribute");
        Console.WriteLine("\t" + e.Attr.Name + " " + e.Attr.InnerXml);
        Console.WriteLine(sender.ToString());
    }

    public static void Serializer_UnknownNode(object sender, XmlNodeEventArgs e)
    {
        Console.WriteLine("Unknown Node");
        Console.WriteLine("\t" + e.Name + " " + e.Text);
        Console.WriteLine(sender.ToString());
    }

    public static void Serializer_Element(object sender, XmlElementEventArgs e)
    {
        Console.WriteLine("Unknown element");
        Console.WriteLine("\t" + e.Element.Name + " " + e.Element.InnerText);
        Console.WriteLine(sender.ToString());
    }

    public static void Serializer_UnreferencedObject(object sender, UnreferencedObjectEventArgs e)
    {
        Console.WriteLine("Serializer_UnreferencedObject");
        Console.WriteLine("\t" + e.UnreferencedId + " " + e.ToString());
        Console.WriteLine(sender.ToString());
    }

The class ‘Form10’ is too big to list here, and was created by copying the contents of C:/Users/Me/file.xml ( which is 2162 lines long ) to the clipboard and using the Edit \ Paste Special \ Paste XML as Classes feature in Visual Studio 2019. This is too big to scan visually for errors, but did include a number of filetypes that I know can be problematic ( eg ushort) .
I set as many of these types to string where possible to offset any data conversion issues.
It also generated a number fields which did not appear to be based on the xml.

When I run the process, it fails with the messages 'There is an error in XML document (6, 3135)’ and’ Input string was not in a correct format’. My reading of the error is that it thinks that there is a problem at column 3135 in line 6 . But Line 6 is only 79 characters long !

I added the static void functions to see if those could provide more information, but they did not help

Is there a way to make the XMLSerializer show more information about what field it doesnt like
Or is there a more robust way to either generate the class, or to parse the XML. Any suggestions gratefully received,
Thanks Richard

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,245 questions
{count} votes