简体   繁体   English

C#XPathDocument(Path)在返回文档之前会评估整个路径吗?

[英]C# does XPathDocument(Path) evaluate the whole path before returning the document?

my English is not he best, but it will work I think. 我的英语不是他最好的,但是我认为它会起作用。 Also I'm an absolut newcomer to C#. 我也是C#的绝对新手。

Given is the following code snippet: It has to open an XML-document, from which I KNOW that one of the nodes can be missintepreted, btw is really wrong. 给出以下代码片段:它必须打开一个XML文档,据我所知,其中一个节点可能会被误解,btw确实是错误的。


try          
{
    XPathDocument expressionLib = new XPathDocument(path);
    XPathNavigator xnav = expressionLib.CreateNavigator();
}

...and so on ...等等


my intention is to create the XPathDocument and the XPathNavigator and THEN watch out for the errors. 我的意图是创建XPathDocument,然后XPathNavigator和THEN会注意错误。

but my code Fails with "XPathDocument expressionLib = new XPathDocument(path);" 但是我的代码因“ XPathDocument expressionLib = new XPathDocument(path);”而失败 (well, it raises an expception which I catch) so I assume that "XPathDocument(path);" (嗯,这引起了我的理解),所以我假设“ XPathDocument(path);” validates the whole XML-document before returning it. 返回之前验证整个XML文档。

At Microsoft pages I didn't find any hints for that assumed behavior - can you verify it? 在Microsoft页面上,我没有找到有关该假定行为的任何提示-您可以验证吗?

And, what could be the workaround? 而且,可能的解决方法是什么?

Yes, I WANT open that XML with that error inside (not at the topmost node) and react just for that invalid node and work with the rest of the file. 是的,我想打开该XML并在内部(而不是在最高节点处)出现该错误,并仅对该无效节点做出反应,并处理文件的其余部分。

Enjoy Weekend Alex. 享受周末亚历克斯。

There is no workaround. 没有解决方法。 If the document is not a valid XML document or there are invalid characters or sections in the document you'll get the exception. 如果该文档不是有效的XML文档,或者该文档中包含无效字符或部分,您将获得异常。

The only way to continue is to handle the XmlException and try to manipulate the Xml data to make it valid which could range from simple if it's just a matter of escaping some invalid character(s) to complex if you have to perform some advanced formatting or if you receive documents containing many different types of errors. 继续的唯一方法是处理XmlException并尝试操纵Xml数据以使其有效,其范围从简单的(如果只是转义一些无效字符)到复杂的(如果必须执行一些高级格式设置)或如果您收到包含许多不同类型错误的文档。

Perhaps the best course of action is to write an XML validator/repair class you'd put your XML document through before attempting to load it with XPathDocument class although I'm pretty sure there must be some library out there that would be able to do all the heavy lifting for you... 也许最好的做法是编写一个XML验证器/修复类,然后再尝试用XPathDocument类加载它,然后将XML文档放入其中,尽管我很确定那里一定有一些库可以执行此操作所有繁重的工作为您...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM