Validate XML

I have an XML string, XMLWebData which may or may not be structured correctly it may or may not be valid XML and need to load it into an XMLDocument. I am using the following:

XMLDoc = new XmlDocument(XMLWebData)

This is all fine unless as I said the XML is invalid and so the parser throws an error. What is the best way to check that XMLWebData is valid XML before creating my XML document?

Can you catch the exception (there used to be a problem where you couldn’t)? If so, let the parser validate it for you.

Just tried that Tim, its still throwing an exception. Tried:

try XMLDoc = new XmlDocument(XMLWebData) catch StatusChange("There was an error obtaining the categories") end try

but getting the following back from the parser “msg:XML parser error 4: not well-formed (invalid token)

You’ll need to add

#pragma breakonexceptions off

before the try to let the exception be caught in the debugger.

[quote=84010:@Wayne Golding]You’ll need to add

#pragma breakonexceptions off

before the try to let the exception be caught in the debugger.[/quote]

That seems to work, thanks wayne

XML parsers are required by the spec to completely reject documents that are not at least well-formed. (What isn’t well-formed isn’t XML.) The hope here is to at least be able to get an exception that Xojo can catch.

Yes well that is working now. It looks like it is parsing up to the (deliberately) malformed part of the XML. Its a pity it is not validated before it is parsed but I guess that would take 2 passes of the XML so I can see why not.