title: C#: shield XmlTextReader from an occasional Unicode character
body: <p>In C#, I have a XmlTextReader created directly from an HTTP response (I have no control over the XML content of the response).</p>
  
  <pre><code>HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  XmlTextReader reader = new XmlTextReader(response.GetResponseStream());
  </code></pre>
  
  <p>It works, but sometimes one of the XML element nodes will contain a Unicode character (e.g. "é") which trips the reader. I've tried to use a StreamReader with declared encoding, but now the XmlTextReader quits out on the very first line: "Data invalid. Line 1, position 1":</p>
  
  <pre><code>StreamReader sReader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Unicode);
  XmlTextReader reader = new XmlTextReader(sReader);
  </code></pre>
  
  <p>Is there a way to fix this? Alternatively, is there a way to prevent the XmlTextReader from parsing an element (I know its name) with a potentially offending character? I don't care about that particular element, I just don't want it to trip the reader.</p>
  
  <p>EDIT: Quick fix: read the response into a StringBuilder ("sb"):</p>
  
  <pre><code>sb.Replace("é", "e");
  StringReader strReader = new StringReader(sb.ToString());
  XmlTextReader reader = new XmlTextReader(strReader);
  </code></pre>