In C #, I have an XmlTextReader created directly from an HTTP response (I have no control over the XML content of the response).
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
XmlTextReader reader = new XmlTextReader(response.GetResponseStream());
It works, but sometimes one of the nodes of the XML elements will contain a Unicode character (for example, "é"), which disables the reader. I tried using StreamReader with the declared encoding, but now XmlTextReader terminates in the very first line: "Data invalid. Line 1, position 1":
StreamReader sReader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Unicode);
XmlTextReader reader = new XmlTextReader(sReader);
Is there any way to fix this? Also, is there a way to prevent the XmlTextReader from parsing an element (I know its name) with a potentially offensive character? I don’t care about this particular element, I just don’t want it to turn off the reader.
EDIT: Quick fix: read the answer in StringBuilder ("sb"):
sb.Replace("é", "e");
StringReader strReader = new StringReader(sb.ToString());
XmlTextReader reader = new XmlTextReader(strReader);
source
share