I am using XML Deserialization for very large XML documents (sometimes over 200MB). The most documents are going very well, but sometimes the Deserialize function throws errors.
For example:
- When an integer is a string, you get the "Wrong format error"
- When a specified type is not recognized, you get an error.
Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseUInt32(String value, NumberStyles options, NumberFormatInfo numfmt)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderFeatureCollection.Read208_Item(Boolean isNullable, Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderFeatureCollection.Read209_Item(Boolean isNullable, Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderFeatureCollection.Read210_Item(Boolean isNullable, Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderFeatureCollection.Read211_Item(Boolean isNullable, Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderFeatureCollection.Read212_Item(Boolean isNullable, Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderFeatureCollection.Read213_Item(Boolean isNullable, Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderFeatureCollection.Read228_TrailInformation(Boolean isNullable, Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderFeatureCollection.Read237_FeatureCollectionFeatureMember(Boolean isNullable, Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderFeatureCollection.Read238_FeatureCollection(Boolean isNullable, Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderFeatureCollection.Read239_FeatureCollection()
The errors are no problem (I know how to handle them), it's very likely we will face other errors in the future which we cannot forsee now.
How can I tell the XMLSerializer to just continue and skip these errors?
I tried the custom eventHandlers, but they won't work.
I might be able to read the error messages and just delete the lines which are giving errors, but that's a really nasty way (See workaround below)!
Code:
try
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(FeatureCollection));
xmlSerializer.UnknownElement += new XmlElementEventHandler(Serializer_ElementEvent);
xmlSerializer.UnknownAttribute += new XmlAttributeEventHandler(Serializer_AttributeEvent);
xmlSerializer.UnknownNode += new XmlNodeEventHandler(Serializer_NodeEvent);
xmlSerializer.UnreferencedObject += new UnreferencedObjectEventHandler(Serializer_UnreferencedObjectEvent);
StringReader reader = new StringReader(xmlstring);
Message = (FeatureCollection)xmlSerializer.Deserialize(reader); // This is where the exception takes place
}
catch (Exception e)
{
System.Windows.MessageBox.Show("Something went wrong.", "Error", System.Windows.MessageBoxButton.OK);
return;
}
Workaround (Work in Progress):
int tries = 1;
while (true)
{
try
{
Bericht = (FeatureCollection)xmlSerializer.Deserialize(reader);
}
catch(Exception e)
{
if(tries >= 100)
{
throw e;
}
Exception innerException = e.InnerException;
// REGEX linenumber innerException.Message; -> Error in XMLDocument (221, 6)
// REMOVE LINE from source
tries++;
continue;
}
break;
}