0

my English is not he best, but it will work I think. Also I'm an absolut newcomer to 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.


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.

but my code Fails with "XPathDocument expressionLib = new XPathDocument(path);" (well, it raises an expception which I catch) so I assume that "XPathDocument(path);" validates the whole XML-document before returning it.

At Microsoft pages I didn't find any hints for that assumed behavior - can you verify it?

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.

Enjoy Weekend Alex.

Dean Kuga
  • 11,878
  • 8
  • 54
  • 108
  • *"it raises an expception which I catch"* -- Can you think of any possible way for you to identify which exception it's throwing, and tell us the type of the exception and the message? Do you think that information might possibly tell you anything about what's going wrong with your code? – 15ee8f99-57ff-4f92-890c-b56153 Apr 06 '17 at 19:35
  • @EdPlunkett There are only two exceptions that constructor can throw. XmlException when there is an error in XML data and ArgumentNullException indicating the file path param is null so it's quite clear what exception he's getting and why... – Dean Kuga Apr 06 '17 at 19:45
  • @DeanKuga I guess I'm the only guy on earth who doesn't have that information committed to memory. Carry on. – 15ee8f99-57ff-4f92-890c-b56153 Apr 06 '17 at 19:49
  • @EdPlunkett No you're not, I didn't have it in my RAM either but it took me about 30 seconds to look it up... – Dean Kuga Apr 06 '17 at 19:53
  • @DeanKuga If the questioner can't be bothered to paste in the error message he's looking at, I've got better things to do than go on google trying to guess what it might be. – 15ee8f99-57ff-4f92-890c-b56153 Apr 06 '17 at 19:57
  • @EdPlunkett No, I agree with you, it's just that in this case it was really easy to know what the problem was even without looking up the constructor on MSDN... – Dean Kuga Apr 06 '17 at 20:06
  • @DeanKuga In this case -- which I'd have known only if I made a habit of looking stuff up on MSDN before asking OP to make the bare minimum effort expected of any question on Stack Overflow. I mean, if you'd gotten to it first and told him what you told him, I wouldn't have stuck my oar in, but that's how the cookie crumbled this time. – 15ee8f99-57ff-4f92-890c-b56153 Apr 06 '17 at 20:09

1 Answers1

0

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.

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.

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...

Dean Kuga
  • 11,878
  • 8
  • 54
  • 108