我正在将数据导入我们的程序。我有一个样式表,可以转换传入的数据以匹配我们的实体框架。将转换后的数据反序列化到我们的实体框架时出现错误。一个或多个字段中的数据格式不正确,可能是空的布尔值或试图进入数字字段的字符数据。实体很大,很难追踪特定的领域。
我想知道异常对象中是否有引用,可以用来帮助跟踪相关字段。
这是错误消息和堆栈跟踪:
There is an error in the XML document.
System.FormatException: 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.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Xml.XmlConvert.ToInt32(String s)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderCrashEntity.Read28_CrashNamesEntity(Boolean isNullable, Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderCrashEntity.Read29_CrashEntity(Boolean isNullable, Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderCrashEntity.Read30_CrashEntity()
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader)
at XMLUtility.DeserializeObject(String XML, Type ObjectType) in XMLUtility.cs:line 155
at DirectoryFramework.ImportData() in DirectoryFramework.cs:line 344
答案 0 :(得分:0)
我能够找到坏演员。我使用了for循环,因为我知道我有多少个空元素,但是您可以改用while循环,直到EOF。
string myXML = outputXml;
int startIndex = 1;
int endIndex = 0;
int insertAt = 0;
string fldLIst = "";
for (int i = 0; i < 98; i++)
{
insertAt = myXML.IndexOf("></", startIndex) + 1;
startIndex = insertAt + 2;
endIndex = myXML.IndexOf(">", startIndex);
string myElement = myXML.Substring(startIndex, endIndex - startIndex);
myXML = myXML.Insert(insertAt, "-9" );
try
{
importedEntity = XMLUtility.DeserializeObject(myXML, entType);
fldLIst += myElement + "\r\n";
}
catch (Exception ex)
{
Console.WriteLine(myElement);
}
}
Console.WriteLine("---------\r\n" + fldLIst); // The first element here is the one that caused the problem.
一旦知道哪个元素不好,就修改样式表以检查是否为空字符串,如果为空则返回0。顺便说一句,可能是导致问题的几个空白元素,您将不得不重复此过程,直到处理完所有元素。