ArgumentException : illegal characters in path

Today my colleague faced an interesting issue, he had build and deployed a an application which has a process which invokes a stored procedure, collects response  and does further processing. While testing he encountered below error in event log 

illegal characters in path

Why it happened

In order to invoke the Stored procedure, schema was generated from it and Message to be sent was constructed with the help of XML document variable.

                                     xDoc = new System.Xml.XmlDocument();

And instance of the SP request schema was loaded in it

                                    xDoc.Load("Generated instance");

And this is the place the issue was - incorrect method usage, correct method to be used is LoadXml().

Question might come in mind, why no error while compiling - it is because Load() and LoadXml() are both valid method for loading XML.

Although the purpose of both method is same i.e. to load XML but it differs in the way they work.
Load() loads from a certain source i.e. either from a stream, TextReader, path/URL, or XmlReader, whereas LoadXml() loads directly from a string i.e. the XML contained within a string.

Thus when used Load method, at runtime XLang engine looks for path of the source but as there was no path it threw an exception - illegal characters in path.

What to do

As here XML was to be read from string, LoadXml() method is the correct one, after doing this correction issue was resolved.

using loadxml method

If you have questions or suggestions, feel free to do in comments section below !!!

Do share if you find this helpful .......
                          Knowledge Sharing is Caring !!!!!!


If you have any suggestions or questions or want to share something then please drop a comment

  1. Thanks pal that works. Took me ages to solve such a basic problem. Lots of threads online using .Load() so I didn't really question it

Previous Post Next Post