Brady Moritz
Brady Moritz

Reputation: 8903

XmlDocument.Load finds illegal characters in my file path

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?

Upvotes: 1

Views: 2102

Answers (2)

CodySig
CodySig

Reputation: 194

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.

Upvotes: 0

tinstaafl
tinstaafl

Reputation: 6948

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));

Upvotes: 1

Related Questions