I am trying to load an HttpWebResponse into an XmlDocument and am getting the exception “Data at the root level is invalid. Line 1, position 1”. If I output the response to the Console I get “system.net.connectstream”. The credentials don’t seem to be my problem because if I enter an incorrect password my exception changes to the 404 error. Here is my code…
string username = "username";
string password = "password";
string url = "https://myurl.com";
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Credentials = new NetworkCredential(username, password);
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
XmlDocument xmlDoc = new XmlDocument();
Console.WriteLine(response.GetResponseStream());
xmlDoc.Load(response.GetResponseStream());
Thanks!
Calling
ToStringonGetResponseStream()isn’t going to do much for you –Stream.ToStringisn’t overridden.I suggest you use something like this for debugging::
Then replace the middle section
(with StreamReader)with theXmlDocument.Loadcall.I suspect you’ll see that it’s basically invalid XML, but the above should show you what it really is.
EDIT: Your comment shows the data as:
That’s JSON. It’s not XML. Don’t try to load it as XML. You have two options: