1

Hi am getting error in the below code as Illegal characters in path, how to clear that?

string pathway = System.IO.File.ReadAllText(@"D:\\Project\\SMCCampaignmgmt\\trunk\\Run\\smccampaignwindows.exe.config");
XmlDocument doc = new XmlDocument();
doc.Load(pathway);
2
  • Which exact line throws the error? Commented Feb 10, 2014 at 9:55
  • doc.Load(pathway) this lines gives me the error. Commented Feb 10, 2014 at 10:00

4 Answers 4

5

The @ character at a beginning of a string means you are declaring a literal string, you don't need to escape it.

Use \ instead of \\ or alternatively remove the @ symbol.


After testing this it appears the double backslash won't actually throw this exception, Windows seems to be clever enough to ignore the extra \. However, you appear to load in another path which you use to load your XML file i.e.

string pathway = ...

The problem is most likely in this file path (which you don't show in your example). That path either has an invalid character in it or possibly due to an encoding issue with how you are reading the file in.


Actually on further review of your code, it looks as though you are trying to load in your app.config file. The Load method of XDocument expects a file path, not raw XML. You have 2 options, use Load correctly by passing the file path directly

doc.Load("D:\\Project\\SMCCampaignmgmt\\trunk\\Run\\smccampaignwindows.exe.config")

Or keep the code as you have it but call the Parse method

string pathway = System.IO.File.ReadAllText(@"D:\\Project\\SMCCampaignmgmt\\trunk\\Run\\smccampaignwindows.exe.config");
XmlDocument doc = XmlDocument.Parse(pathway);
Sign up to request clarification or add additional context in comments.

5 Comments

Yes, double backslashes are silently treated as single backslashes by the Windows API.
Just tested this and you are correct...hmmm. Let me have another look at the OP's code...
i replaced \ with \\ it shows me unrecognised escape sequence.
@user3226735 that's because you have also removed the @ - like I said you either remove the extra \ or remove the @ - not both. Anyway, see my update.
@user3226735 see update, think I can see what your problem is now.
2

If you use @ to escape an entire string, you shouldn't escape the \ manually inside that string.

Comments

0

I can't see any illegal characters in that path, but give this a go

doc.Save(@"D:\Project\SMCCampaignmgmt\trunk\Run\smccampaignwindows.exe.config");

1 Comment

I tried removing @ but still it shows me error in doc.Load(pathway)
0

In pathway you load an entire file (ReadAllText). The illegal character is probably in the contents of that file (i.e in D:\Project\SMCCampaignmgmt\trunk\Run\smccampaignwindows.exe.config)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.