[翻译]  Can't seem to handle XMLException?

[CHINESE]  似乎无法处理XMLException?


I have the following code in one of our projects webpages:

我在我们的一个项目网页中有以下代码:

            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(File.FullName);

            //work through each print batch in this queue file
            try
            {
                XmlNodeList nodeList = xDoc.SelectNodes("Reports/PrintBatch");
                foreach (XmlNode printBatch in nodeList)//xDoc.SelectNodes("Reports/PrintBatch"))
                {
                    PrintBatch batch = new PrintBatch();
                    batch.LoadBatch(printBatch, File.Extension);
                    this.AddBatch(batch);
                }
            }
            catch (XmlException e)
            {
                //this report had an error loading!
                Console.WriteLine(e.Message);
            }

It basically takes an xml batch file and loads it up as an object, ready to be processed.

它基本上需要一个xml批处理文件并将其作为对象加载,准备处理。

It's been working fine, until recently when one of the XML files was found to contain a null character (which is invalid in XML).

它一直工作正常,直到最近才发现其中一个XML文件包含空字符(在XML中无效)。

When it tries to process this "dudd" file, we get the following exception:

当它试图处理这个“dudd”文件时,我们得到以下异常:

alt text http://blog.ianmellor.co.uk/images/xml_err.jpg

alt text http://blog.ianmellor.co.uk/images/xml_err.jpg

Ok so far.. but when we then try to "continue" or "step over", I expect it to flow into the catch block. However, it doesn't; we simply get the red screen of death:

好到目前为止..但是当我们尝试“继续”或“跳过”时,我希望它会流入catch块。但是,它没有;我们只是得到死亡的红屏:

alt text http://blog.ianmellor.co.uk/images/xml_err2.jpg

alt text http://blog.ianmellor.co.uk/images/xml_err2.jpg

What am I doing wrong?

我究竟做错了什么?

2 个解决方案

#1


5  

It is because you have not written

这是因为你没有写

xDoc.Load(File.FullName);

inside the try block. That is the reason why the exception was not handled.

在try块内。这就是为什么不处理异常的原因。

#2


2  

The other answer about putting Load() inside the try block is right, but doesn't actually explain why SelectNodes() "appears" to be throwing an XmlException that is not being caught.

将load()放在try块中的另一个答案是正确的,但实际上并没有解释为什么SelectNodes()“出现”抛出一个未被捕获的XmlException。

The actual answer is that the debugger is confused/out of sync with your source code and is actually showing the wrong line as causing the exception.

实际答案是调试器与源代码混淆/不同步,实际上显示错误的行导致异常。

It should really be pointing to the xDoc.Load(File.FullName); , in which case it would be clear that this call should be inside the try block.

它应该指向xDoc.Load(File.FullName); ,在这种情况下,很明显这个调用应该在try块内。

Why? Notice the XmlLoader.LoadNode() in the last line of the stack trace. In .NET Reflector you can see that the XmlDocument.Load() method (deep in it's bowels) calls the LoadNode() method.

为什么?注意堆栈跟踪的最后一行中的XmlLoader.LoadNode()。在.NET Reflector中,您可以看到XmlDocument.Load()方法(深入它的大小)调用LoadNode()方法。

However, also in reflector, it can be seen that the SelectNodes() method does not call LoadNode() anywhere in it's internal implementation.

但是,同样在反射器中,可以看出SelectNodes()方法不会在其内部实现中的任何位置调用LoadNode()。

So according to the stack trace, the exception cannot have been caused by SelectNodes().

因此,根据堆栈跟踪,异常不能由SelectNodes()引起。

I've seen the debugger get confused like this before when a code change is made and debugging started, but the debugging symbols have not been updated correctly. Try cleaning and re-building your solution to refresh the debugging symbols.

我已经看到调试器在进行代码更改和调试开始之前就像这样混淆,但是调试符号没有正确更新。尝试清理并重新构建解决方案以刷新调试符号。


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2018 ITdaan.com 粤ICP备14056181号