I'm trying to convert web response to xml. However, even if xml string is validated, i'm getting "invalid character error" when i try to convert response string to XmlDocument. I also apply the accepted answer in here. Code is below:
public XmlDocument RequestAndResponseHelper(string requestStr, string directory)
{
var request = (HttpWebRequest)WebRequest.Create(directory);
var data = Encoding.ASCII.GetBytes(requestStr);
request.Method = "POST";
request.ContentType = "text/xml";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
//Accepted answer applied
StringBuilder newString = new StringBuilder();
char ch;
for (int i = 0; i < responseString.Length; i++)
{
ch = responseString[i];
// remove any characters outside the valid UTF-8 range as well as all control characters
// except tabs and new lines
//if ((ch < 0x00FD && ch > 0x001F) || ch == '\t' || ch == '\n' || ch == '\r')
//if using .NET version prior to 4, use above logic
if (XmlConvert.IsXmlChar(ch)) //this method is new in .NET 4
{
newString.Append(ch);
}
}
String newStr = newString.ToString();
XmlDocument rs = new XmlDocument();
rs.Load(newStr);
return rs;
}
In both cases(applying the code in linked answe or not applying that), Xml strings are valid and the same.
Can you suggest any other solution?

You're using XmlDocument.Load, which is meant to accept a URL. I believe you meant to use XmlDocument.LoadXml which parses the text as XML.
(As an aside, I'd strongly recommend updating to use XDocument if you possibly can. LINQ to XML is a much nicer XML API.)
See more on this question at Stackoverflow