-1

I want to use

XmlReader.Create(aString);

to read XML. but there are many "\" in this aString, which cause an error:

Illegal characters in path.

So I want to replace all "\" with "".

I've tried:

aString.Replace("\", "");
aString.Replace("\\", "");
aString.Replace(@"\", "");
aString.Replace(@"\", string.Empty);

None of them works.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
howexg9
  • 93
  • 1
  • 6
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jun 20 '13 at 17:33
  • 1
    Are you *sure* the backslashes are really there? If so, shouldn't you be going through some uniform unescaping first, rather than just backslashes? Also note that you're currently ignoring the return value of `Replace`, which isn't going to work. If you could give us more context, we may well be able to suggest a better option. – Jon Skeet Jun 20 '13 at 17:33
  • 2
    What goes wrong? Are you assigning the value of `aString.Replace("\\","");` back into aString? – Mike Precup Jun 20 '13 at 17:33
  • 2
    How do you end up with a badly formed string in the first place? Maybe the better fix is upstream. – Damien_The_Unbeliever Jun 20 '13 at 17:34
  • Both `@"\"` and `"\\"` should work. – abatishchev Jun 20 '13 at 17:36
  • @dasblinkenlight: You're marking as a duplicate of **closed as duplicate** question ;) – abatishchev Jun 20 '13 at 17:36
  • You have your answer here: http://stackoverflow.com/questions/10752852/replacing-backslash-in-a-string – Lucas Jun 20 '13 at 17:37
  • @abatishchev This question has been asked so many times, it's no wonder that sooner or later one would close a dupe pointing to another dupe :) – Sergey Kalinichenko Jun 20 '13 at 17:40
  • You can look at this for an alternative method of loading a XML file [Illegal characters in path error while parsing XML in C#](http://stackoverflow.com/questions/1374729/illegal-characters-in-path-error-while-parsing-xml-in-c-sharp) – Gerhard Powell Jun 20 '13 at 20:14

3 Answers3

7

You're probably not capturing the output - Replace does not modify the existing string - it returns a new string. Try:

aString = aString.Replace(@"\","");
D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • This is immediately what I thought after looking at the OP – Saggio Jun 20 '13 at 18:01
  • 1
    This is what I use in my code which doesn't work: string aString=model.wresult.ToString(); aString=aString.Replace(@"\",""); XmlReader reader=XmlReader.Create(aString); – howexg9 Jun 20 '13 at 18:36
  • 1
    What is your input string? Are you sure the "\" character is the only illegal character? – D Stanley Jun 20 '13 at 18:49
0

You're not assigning the output of String.Replace() method anywhere. Replace method does not modify the object.

Peuczynski
  • 4,591
  • 1
  • 19
  • 33
  • This is what I use in my code, which doesn't work: string aString=model.wresult.ToString(); aString=aString.Replace(@"\",""); XmlReader reader=XmlReader.Create(aString); – howexg9 Jun 20 '13 at 18:34
0

I see that there are \" in your code. That is how VisualStudio shows ". (The \ is in the case a escape char and not the actual char. The data in aString is correct and there is no reason to try to remove the \ from aString. The problem lays with what you are doing with aString, and not with the \ inside aString.

Please note: XmlReader.Create(aString); reads a XML file from the path aString. XmlDocument.LoadXml(string) parse a XML file.

Gerhard Powell
  • 5,965
  • 5
  • 48
  • 59