title: C# remove special characters from string
body: <p>I have the following string which represents an xml:</p>
  
  <pre><code>string xmlStr7 = "&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;Response xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"&gt;\r\n  &lt;Market&gt;en-US&lt;/Market&gt;\r\n  &lt;AnswerSet ID=\"0\"&gt;\r\n    &lt;Answers&gt;\r\n      &lt;Answer ID=\"0\"&gt;\r\n        &lt;Choices&gt;\r\n          &lt;Choice ID=\"2\" /&gt;\r\n          &lt;Choice ID=\"8\" /&gt;\r\n        &lt;/Choices&gt;\r\n      &lt;/Answer&gt;\r\n      &lt;Answer ID=\"1\"&gt;\r\n        &lt;Choices&gt;\r\n          &lt;Choice ID=\"1\" /&gt;\r\n          &lt;Choice ID=\"4\" /&gt;\r\n        &lt;/Choices&gt;\r\n      &lt;/Answer&gt;\r\n      &lt;Answer ID=\"2\"&gt;\r\n        &lt;Choices&gt;\r\n          &lt;Choice ID=\"1\" /&gt;\r\n          &lt;Choice ID=\"7\" /&gt;\r\n        &lt;/Choices&gt;\r\n      &lt;/Answer&gt;\r\n      &lt;Answer ID=\"3\"&gt;\r\n        &lt;Choices&gt;\r\n          &lt;Choice ID=\"4\" /&gt;\r\n        &lt;/Choices&gt;\r\n      &lt;/Answer&gt;\r\n    &lt;/Answers&gt;\r\n  &lt;/AnswerSet&gt;\r\n&lt;/Response&gt;";
  </code></pre>
  
  <p>I want to parse it into an XDocument object and in order to do so I must get rid of all the newlines and unnecessary spaces (otherwise I get a parsing error).
  I've removed the special characters <strong>manually</strong> and saw that the parsing works when I use the following string:</p>
  
  <pre><code>string xmlStr2 = "&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;&lt;Response xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"&gt;&lt;Market&gt;en-US&lt;/Market&gt;&lt;AnswerSet ID=\"0\"&gt;&lt;Answers&gt;&lt;Answer ID=\"0\"&gt;&lt;Choices&gt;&lt;Choice ID=\"2\" /&gt;&lt;Choice ID=\"8\" /&gt;&lt;/Choices&gt;&lt;/Answer&gt;&lt;Answer ID=\"1\"&gt;&lt;Choices&gt;&lt;Choice ID=\"1\" /&gt;&lt;Choice ID=\"4\" /&gt;&lt;/Choices&gt;&lt;/Answer&gt;&lt;Answer ID=\"2\"&gt;&lt;Choices&gt;&lt;Choice ID=\"1\" /&gt;&lt;Choice ID=\"7\" /&gt;&lt;/Choices&gt;&lt;/Answer&gt;&lt;Answer ID=\"3\"&gt;&lt;Choices&gt;&lt;Choice ID=\"4\" /&gt;&lt;/Choices&gt;&lt;/Answer&gt;&lt;/Answers&gt;&lt;/AnswerSet&gt;&lt;/Response&gt;";
  </code></pre>
  
  <p>I use the following code to achieve this programatically:</p>
  
  <pre><code>public static string replaceSubString(string st)
      {
          string pattern = "&gt;\\s+&lt;";
          string replacement = "&gt;&lt;";
          Regex rgx = new Regex(pattern);
          string result = rgx.Replace(st, replacement);
          return result;
      }
  </code></pre>
  
  <p>By calling this method I expect to get a string that I will be able to parse to an XDocument object:</p>
  
  <pre><code>string newStr = replaceSubString(xmlStr7);
  XDocument xmlDoc7 = XDocument.Parse(newStr);
  </code></pre>
  
  <p>However, this does not work.
  In addition, there seem to be a difference between this string and the string xmlStr2 from which I removed all the special characters manually (string.Compare returns false and newStr is longer in 1 char than xmlStr2). I can't see this difference by printing both strings, they seem identical.</p>