RichardScannell-9815 avatar image
0 Votes"
RichardScannell-9815 asked JialeXueMSFT-4441 commented

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

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

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

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

     public static void Serializer_UnreferencedObject(object sender, UnreferencedObjectEventArgs e)
         Console.WriteLine("\t" + e.UnreferencedId + " " + e.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

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Richard,

Use Exception.InnerException Property.

During the deserialization process, only a few error conditions can cause exceptions. The most common conditions are:

  1. The name of the root element or its namespace does not match the expected name.

  2. Enumerated data types present undefined values.

  3. The document contains illegal XML.

Just like in the case of serialization, whenever a problem occurs, the Deserialize() method will raise an InvalidOperation exception with the following message:

There is an error in XML document (, ).

This exception usually contains the real exception in the InnerException property. The type of InnerException varies depending on the actual error that occurs when reading the XML document. If the serializer cannot match the document with the type passed to the constructor, the type specified by the XmlInclude attribute, or the type specified in the Type[] of a more complex overload passed to the XmlSerializer constructor The root element of the InnerException is InvalidCastException. XmlSerializer will look at Qname (the name of the element) and namespace to determine which class to deserialize the document into. They must all match the declaration in the .NET class .

If that does not work, please provide the structure and code of the XML file and form10 so that I can reproduce your problem.

Best regards,
Jiale xue

0 Votes 0 ·

0 Answers