来自HttpWebResponse的XML字符串和对象模型转换

时间:2022-08-13 07:31:16

I have a requirement, to print XML string as well as converting object model from a HttpResponse. I have written the following code for that:

我有一个要求,打印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);

The problem is if I convert the response to XML string in between, the code encounters errors at the point of object model conversion. The error is:

问题是如果我将响应转换为XML字符串,则代码在对象模型转换时遇到错误。错误是:

 There is an error in XML document (0, 0). ---> System.Xml.XmlException: Root element is missing.

What is the better way to implement this? Also I tried to copy the HttpResponse in a temporary variable and tried to use it further, but that is also not working. Any suggestions?

实现这个的更好方法是什么?此外,我试图在一个临时变量中复制HttpResponse,并尝试进一步使用它,但这也无法正常工作。有什么建议?

1 个解决方案

#1


0  

I think that your string is null thats why you get the XmlException. Try to print the response and see if that is != null. Also shouldn't you decode the response?

我认为你的字符串是null,这就是为什么你得到XmlException。尝试打印响应,看看是否是!= null。你不应该解码响应吗?

HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url); 
            // Sends the HttpWebRequest and waits for the response.         
            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); 
            // Gets the stream associated with the response.
            Stream receiveStream = myHttpWebResponse.GetResponseStream();
            Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
            // Pipes the stream to a higher level stream reader with the required encoding format. 
            StreamReader readStream = new StreamReader( receiveStream, encode );
        Console.WriteLine("\r\nResponse stream received.");
            Char[] read = new Char[256];
            // Reads 256 characters at a time.     
            int count = readStream.Read( read, 0, 256 );
            Console.WriteLine("HTML...\r\n");
            while (count > 0) 
                {
                    // Dumps the 256 characters on a string and displays the string to the console.
                    String str = new String(read, 0, count);
                    Console.Write(str);
                    count = readStream.Read(read, 0, 256);
                }
            Console.WriteLine("");
            // Releases the resources of the response.
            myHttpWebResponse.Close();
            // Releases the resources of the Stream.
            readStream.Close();

Also checkout : http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.getresponsestream.aspx Hope this helps!

还有结帐:http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.getresponsestream.aspx希望这有帮助!

#1


0  

I think that your string is null thats why you get the XmlException. Try to print the response and see if that is != null. Also shouldn't you decode the response?

我认为你的字符串是null,这就是为什么你得到XmlException。尝试打印响应,看看是否是!= null。你不应该解码响应吗?

HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url); 
            // Sends the HttpWebRequest and waits for the response.         
            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); 
            // Gets the stream associated with the response.
            Stream receiveStream = myHttpWebResponse.GetResponseStream();
            Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
            // Pipes the stream to a higher level stream reader with the required encoding format. 
            StreamReader readStream = new StreamReader( receiveStream, encode );
        Console.WriteLine("\r\nResponse stream received.");
            Char[] read = new Char[256];
            // Reads 256 characters at a time.     
            int count = readStream.Read( read, 0, 256 );
            Console.WriteLine("HTML...\r\n");
            while (count > 0) 
                {
                    // Dumps the 256 characters on a string and displays the string to the console.
                    String str = new String(read, 0, count);
                    Console.Write(str);
                    count = readStream.Read(read, 0, 256);
                }
            Console.WriteLine("");
            // Releases the resources of the response.
            myHttpWebResponse.Close();
            // Releases the resources of the Stream.
            readStream.Close();

Also checkout : http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.getresponsestream.aspx Hope this helps!

还有结帐:http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.getresponsestream.aspx希望这有帮助!