.net - 是否有办法确定哪个元素导致.net中的反序列化错误

标签 .net xslt error-handling

我正在将数据导入我们的程序。我有一个样式表,可以转换传入的数据以匹配我们的 Entity Framework 。将转换后的数据反序列化到我们的 Entity Framework 时出现错误。一个或多个字段中的数据格式不正确,可能是空的 bool(boolean) 值或试图输入数字字段的字符数据。实体很大,很难追踪特定的 Realm 。

我想知道异常对象中是否有引用,可以用来帮助跟踪相关字段。

这是错误消息和堆栈跟踪:

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

最佳答案

我找到了坏 Actor 。我使用了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。顺便说一句,可能是导致问题的几个空白元素,您将不得不重复此过程,直到处理完所有元素。

关于.net - 是否有办法确定哪个元素导致.net中的反序列化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56694429/

相关文章:

.net - 寻找可能的 MethodAttributes.SpecialName 的确切列表

.net - native 代码、机器代码和汇编代码有什么区别?

.net - 使用 Oslo 和 M 有什么优势,你会在什么时候使用它?

java - Spring MVC @RequestBody 用于 JAXB 域对象 接收到的 XML 的 XSLT 转换后

angular - 错误处理Angular 6

Android setError ("error") 在 Textview 中不起作用

.net - CS1607 : The version specified for the 'file version' is not in the normal 'major.minor.build.revision' format in . 网络

html - 通用 xml -> 使用 xslt 和约定的 html 转换?

xml - 每次模板运行时 XSL 自动递增一个数字

ruby-on-rails - Rails - Simple Form Bootstrap - 显示内联错误的全文 -(或任何地方)