1

I have a file with xml in it. The path contains a character "ñ" in it, but the full path was url encoded before being saved to the file, so this character is percent encoded along with a number of other characters in the path.

I try to load the file with the following code, and the Exists portion succeeds, but then the Load() call fails with a

System.ArgumentException: Illegal characters in path.

if (File.Exists(path))
{
    var xd = new XmlDocument();
    xd.Load(path); 
}

I have a bunch of these files with the url encoding being used, but it's only these accented characters that cause problems.

Any ideas?

Chandan Kumar
  • 4,570
  • 4
  • 42
  • 62
Brady Moritz
  • 8,624
  • 8
  • 66
  • 100
  • Duplicate of: http://stackoverflow.com/questions/7496913/how-to-load-xml-from-url-on-xmldocument – Arnaud F. Mar 31 '17 at 04:27
  • Possible duplicate of [How to load XML from URL on XmlDocument()](http://stackoverflow.com/questions/7496913/how-to-load-xml-from-url-on-xmldocument) – Arnaud F. Mar 31 '17 at 04:28
  • Can you successfully open the file with [`var stream = File.OpenRead (path)`](https://msdn.microsoft.com/en-us/library/system.io.file.openread(v=vs.110).aspx) and then load from the stream using [this overload](https://msdn.microsoft.com/en-us/library/e48zttz7(v=vs.110).aspx)? – dbc Mar 31 '17 at 04:28
  • Can you show that path in the post? Url-encoding local file path makes no sense and without sample question is very confusing. – Alexei Levenkov Mar 31 '17 at 04:37
  • -not a duplicate, as I'm using Load() with a local file system path. not a url and not an xml string. -I am trying the file.openread route next, I'm fairly sure it will work -I'm using query string encoding as I'm working with local files that were copied from actual website urls. – Brady Moritz Mar 31 '17 at 15:22

2 Answers2

1

If I'm not mistaken, your problem has to do with encoding. the Load method defaults to UTF-8, which interprets the special characters differently. One workaround for this, would be to pass IO.Files.ReadAllText(path) to the LoadXml method of the XmlDocument:

xd.LoadXml(IO.Files.ReadAllText(path));
tinstaafl
  • 6,908
  • 2
  • 15
  • 22
0

It could possibly be that your file path is something in the format of: ...\User\Documents.... Try to add an extra '\' at each directory change. For example: ..\\User\\Documents.

CodySig
  • 174
  • 1
  • 4
  • 15
  • I have thousands of other files in the same path that work fine, so it's definitely just hte accented character causing the issue – Brady Moritz Mar 31 '17 at 15:18