Reputation: 11
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);
Upvotes: 1
Views: 2911
Reputation: 82136
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);
Upvotes: 5
Reputation: 425
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)
Upvotes: 0
Reputation: 4081
If you use @ to escape an entire string, you shouldn't escape the \ manually inside that string.
Upvotes: 2
Reputation:
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");
Upvotes: 0