Click here to Skip to main content
15,881,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
the code is given below
string m_URL = "";
string xmlStr;
using (var wc = new WebClient())
    wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows; Windows NT 5.1; rv: Gecko/20100611 Firefox/3.6.4");
    xmlStr = wc.DownloadString(m_URL);
var xmlDoc = new XmlDocument();

var bdata = XDocument.Load(xmlStr);

In the above code bdata the illegal charecters in path how to remove the illegal characters.

Illegal characters in path
Thanks in Advance...
Updated 8-Oct-15 1:39am
Andreas Gieriet 8-Oct-15 5:46am    
Code block formatting added.
Andreas Gieriet 8-Oct-15 5:47am    
What are "illegal" characters?
What is the problem?
Athul MS 8-Oct-15 5:57am    
hi,Andreas Gieriet the code is changed pls and give me a solution
[no name] 8-Oct-15 6:04am    
Its working at our end. Document is loaded perfectly.
Athul MS 8-Oct-15 6:07am    
in bdata contains an error illegal charecters in path

In your case, you are fetching the data from a website, and using that as a "path" to a file.

So the illegal characters are part of the data you fetched from the site, and beyond our - or your - control.
The chances are that the data you have fetched is not a valid path at all - it's probably XML or HTML data.
So use the debugger, look at exactly what you are fecting and see what the data actually is. Then you can start deciding what is and isn't a "legal character" in your path.
Share this answer
Hi Athul,
XDocument Do not have any suitable method which can take whole xml string as input and load that xml. It have only method in which you have to pass URL of the source.

Please see the screen shot.

Screen Shot[^]
Share this answer
Athul MS 8-Oct-15 6:45am    
is it possible to pass the URL source in Xdocument
[no name] 8-Oct-15 6:57am    
Yes you can pass URL("") directly but you can not pass your header key
wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows; Windows NT 5.1; rv: Gecko/20100611 Firefox/3.6.4");
So it will give you error like Unauthorized access or Forbidden.
string illegal = "\"M\"\\a/ry/ h**ad:>> a\\/:*?\"| li*tt|le|| la\"mb.?";
string invalid = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());

foreach (char c in invalid)
    illegal = illegal.Replace(c.ToString(), "");
Share this answer
Rakesh R Surve[RST] 8-Oct-15 6:11am    
Try this while it may helpful to you...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900