我有一个要求,打印 XML 字符串以及从 HttpResponse 转换对象模型。我为此编写了以下代码:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sURL);
HttpWebResponse tmpResponse = null;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
//Copying the response
//tmpResponse = response;
//Response to XML string
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
sResult = sr.ReadToEnd();
}
//Reponse to Object model
objectmodel = convert(response);
问题是如果我在两者之间将响应转换为 XML 字符串,代码在对象模型转换时会遇到错误。错误是:
There is an error in XML document (0, 0). ---> System.Xml.XmlException: Root element is missing.
实现这一点的更好方法是什么?我还尝试将 HttpResponse 复制到一个临时变量中并尝试进一步使用它,但这也不起作用。有什么建议么?