I have encountered a strange bug while loading a file into IXMLDOMDocument2. The method load returns False, if the file name contains % and sets parseError to:
The system cannot locate the resource specified.
The file exists and when remove % from its name, it works just fine.
Is there any way to load XML file with % in its name?
The documentation for
IXMLDOMDocument.load()says that thexmlSourcesource parameter accepts one of the following values:IStreamISequentialStreamIPersistStreamSAFEARRAYof bytesIXMLDOMDocumentinstanceYou are trying to pass a file name, which the method treats as a URL (the first option in the list above). Character
%in URLs is reserved for percent-encoding. TheIXMLDOMDocument.load()method is trying to decode the URL from the string you passed as an argument, which may result in a slightly different file name. The tricky part is that this doesn't happen when the percent character isn't followed by a pair of hexadecimal digits, meaning that:Test%.xmlwill be decoded asTest%.xml, butTest%21.xmlwill be decoded asTest!.xml, orTest%4A.xmlwill be decoded asTestJ.xml.You should be careful what you pass to the
IXMLDOMDocument.load()method. You have several options.Encode the file name as a URL
This is easy. You can use
TNetEncoding.URLfrom theSystem.NetEncodingunit:Pass an
IStreamas an argument:Open a file using
TFileStream(orTFile.OpenRead()from theSystem.IOUtilsunit) and wrap it in aTStreamAdapterfrom theSystem.CLassesunit, which implements theIStreaminterface declared in theWinapi.ActiveXunit.Use the
loadXML()method instead:There is an
IXMLDOMDOcument.loadXML()method that allows you to load XML content from a string. You can useTFile.ReadAllText()(unitSystem.IOUtils) to read the content of the file and pass it to theloadXML()method.I don't recommend loading XML using this option, because it loads the whole content of the file into memory.