C# 如何调试反序列化异常? [英] C# how can I debug a deserialization exception?

查看:115
本文介绍了C# 如何调试反序列化异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 XSD.exe 工具创建了一个表示 XSD 的 C# 类.我使用验证代码来检查 XML 与 XSD 的一致性.我可以正常工作,但使用另一个 XML 文件会导致异常.

I havve created a C# class representing an XSD using the XSD.exe tool. I use a valdiation code to check the consistency of the XML with the XSD. I got this working, but using another XML file causes exceptions.

XML 文件是由外部程序生成的,我无权访问原始代码或已发布的 XSD.

The XML files are produced by an external program and I do not have access to the original code or to a published XSD.

在反序列化期间读取 XML 时出现异常:

During reading the XML during Deserialization I get an exception:

enter System.InvalidOperationException was unhandled

HResult=-2146233079Message=Het XML-document (235, 17) 包含错误.

HResult=-2146233079 Message=Het XML-document (235, 17) contains an error.

该错误被描述为试图将字符串转换为 DateTime 格式(这不是正确的描述).

The error is described as an attempt to convertt a string to a DateTime format (which cannt be a correct description).

我认为 (235,17) migt 表示在文档中的位置,但这与调用堆栈不一致.

I thougth (235,17) migt represent the location in the document, but this is not consistent with the call stack.

我的问题:你能帮我针对这类问题制定一个好的调试策略吗?我想确切地知道异常发生在 XML 中的哪一行,但不知道如何做到这一点.

My question: can you help me with a good debugging strategy for this type of problems? I would like to know exactly at which line in the XML the exception occurs, but don't know how to do this.

推荐答案

通过 IXmlLineInfo 接口,其中 XmlTextReader 实现.值得注意的是,行号行位置 从第一个开始(只有在不可用时才为零).

Line and character numbers are reported to the exception message via the IXmlLineInfo interface, which XmlTextReader implements. Of note, both the line number and line position start with number one (and are zero only when not available).

因此 "(235, 17)" 是当错误被抛出时阅读器所在行的行号和位置(从 1 开始).在大多数情况下,这将是发生错误的 XML 文件中的实际位置.但是,有时 XML 序列化程序会将阅读器推进到下一个 node 在抛出错误之前.来自实验:

Thus "(235, 17)" are the line number and position in the line (starting at one) at which the reader was positioned when the error was thrown. In most cases, that will be actual position in the XML file at which the error occurs. However, sometimes the XML Serializer will have advanced the reader to the next node before the error is thrown. From experimentation:

  1. 如果 XML 格式不正确,那么错误将是线和位置报告的位置.

  1. If the XML is not well-formed, then the error will be where the line and position report.

如果 XML 格式正确,但属性值无法反序列化,那么具有错误值的属性将是行和位置报告的位置.

If the XML is well formed but an attribute value cannot be deserialized, then the attribute with the bad value will be where the line and position report.

但是,如果 XML 格式良好并且元素值无法反序列化,则 XML 阅读器将已经从元素的末尾前进到下一个元素的开头XML 读取器节点(通常是打开或关闭的元素),因此导致问题的元素将是紧接在报告位置之前 的元素,可能在上一行.

However, if the XML is well-formed and an element value cannot be deserialized, the XML reader will already have been advanced past the end of the element to the beginning of the next XML reader node (typically an element opening or closing), so the element causing the problem will be one one immediately before the reported location, possibly on the previous line.

如果在 (0, 0) 处报告错误,则阅读器无法开始解析 XML.可能的问题包括:

If the error is reported at (0, 0) then the reader was unable to begin parsing the XML. Likely problems include:

  • The encoding is wrong. See for instance How to change character encoding of XmlReader for an example.
  • You are reading from a string using StringReader and there is a BOM embedded at the beginning. See for instance XmlReader breaks on UTF-8 BOM for details.
  • The input is not XML at all. E.g. it's actually JSON, or a text message associated to an error response from a server.

这篇关于C# 如何调试反序列化异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆