1

I'm getting the HttpWebResponse as XML but it doesn't even start to look like an XML document because returned XML doesn't include the header

<?xml version='1.0' encoding='UTF-8'?>

and I am not able to parse it. It is throwing an exception

data at the root level is invalid line 1 position 1

or

System.Xml.XmlException: Root element is missing.

I tried all cases. I am not able figure this out.

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
    StreamReader responsereader = new StreamReader(response.GetResponseStream());
    string responsedata = responsereader.ReadToEnd();
    XmlDocument xmldoc = new XmlDocument();
    xmldoc.load(responsedata);
}

The document I'm passing to the load() method is along the lines:

<ns2:HotelListResponse>
    <customerSessionId>0ABAA82C-EBAC-4791-3C22-4DEFAE‌​D93172</customerSessionId>
    <numberOfRoomsRequested>0</numberOfRoomsRequested>
    <more‌​ResultsAvailable>false</moreResultsAvailable>
    <HotelList activePropertyCount="198" size="198">
        <HotelSummary order="0">
            <hotelId>195974</hotelId>
    ...
</ns2:HotelListResponse>

I've provided just a snippet because of its length.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user88
  • 39
  • 3
  • 7
  • Please post your XML document you're having problems with. – andr Jan 11 '13 at 11:12
  • my xml documnet is httpwebresponse and it is too long. I post some par t of xml with root element.0ABAA82C-EBAC-4791-3C22-4DEFAED931720false195974 – user88 Jan 11 '13 at 11:40
  • You should put such information in the question, as an edit, so that anyone reading will have a chance to see it. Can you post whole document contents to pastebin and provide a link here? – andr Jan 11 '13 at 11:56
  • http://pastebin.com/0Wg8uypv – user88 Jan 11 '13 at 12:19
  • Please also post contents of the whole `HttpWebResponse` contents as a separate pastebin. – andr Jan 11 '13 at 12:21
  • http://api.ean.com/ean-services/rs/hotel/v3/list/?&type=xml&apiKey=tzyw4x2zspckjayrbjekb397&sig=a6f828b696ae6a9f7c742b34538259b0&cid=411931&minorRev=[12]&customerUserAgent=[hotel]&locale=en_US&currencyCode=INR&poststringroom1=2&fromdat=01/11/2013&todate=01/17/2013&city=Mumbai – user88 Jan 11 '13 at 12:26
  • http://pastebin.com/CCxG7Wvk – user88 Jan 11 '13 at 12:45
  • possible duplicate of [Data at the root level is invalid. Line 1, position 1 -why I get this error while load xml file](http://stackoverflow.com/questions/7544475/data-at-the-root-level-is-invalid-line-1-position-1-why-i-get-this-error-whil) – John Saunders Jan 11 '13 at 14:30

1 Answers1

2

The XmlDocument.Load method loads from a stream or other source of bytes. You want XmlDocument.LoadXml.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • I tried but It's not worikng for me. XmlDocument xmldoc = new XmlDocument(); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { StreamReader responsereader = new StreamReader(response.GetResponseStream()); string responsedata = responsereader.ReadToEnd(); xmldoc.LoadXml(responsedata); } – user88 Jan 11 '13 at 14:30
  • Then take a look at `responsedata` before you load it. Use the debugger, which can display data as XML. Alternatively, use `xmldoc.Load(resposeReader)` – John Saunders Jan 11 '13 at 14:32
  • I got this Exception : Invalid URI: The Uri string is too long. actually I got the xml as response. but I think my response xml not contain this header that's why I am not able to parse. if you have any solution so please post it. – user88 Jan 11 '13 at 14:39
  • Try adding the appropriate header and see if it parses then. – John Saunders Jan 11 '13 at 14:41
  • Yup, that's probelm. how to add the header in response xml. I tried lot but I am not able to do this. If you know Please tell me how to do this. – user88 Jan 11 '13 at 14:43
  • `responseData = ""+responseData;` – John Saunders Jan 11 '13 at 14:44
  • No, It's give Invalid URI: The Uri string is too long. – user88 Jan 11 '13 at 14:47
  • When I tried this xmldoc.Loadxml(responsedata). It's give Data at the root level is invalid. Line 1, position 39. – user88 Jan 11 '13 at 14:49
  • What's at position 39? Perhaps it sees something that it thinks is the beginning of a URI that is too long? – John Saunders Jan 11 '13 at 18:31
  • Don't worry about position 39. It's my program line number. URI is too long because I am passing my response data directly not the URL so I am not able to find the exact solution. – user88 Jan 12 '13 at 07:19
  • It's not your code line number! It's the position within the XML document on line 1! Thanks for playing. Always wanted you to tell me what to worry about ;-) – John Saunders Jan 12 '13 at 10:16
  • Ok,Thanks John but What is the solution of this probelm. do you help me in this regards. – user88 Jan 12 '13 at 10:29
  • When i am using xmldoc.LoadXml(responsereader.ReadToEnd()); It's give data at the root level is invalid line 1 position 1. can you tell me. Where I am wrong. – user88 Jan 12 '13 at 13:13
  • 1. Put responseReader.ReadToEnd() into a variable. 2. Use the debugger to display the variable. 3. Copy from the displayed variable into a text file something.xml. 4. Open that file in Visual Studio or some other XML editor and see what it says. – John Saunders Jan 12 '13 at 13:45
  • I tried this statement,I post pastebin link. this link contain the respons. StreamReader responsereader = new StreamReader(response.GetResponseStream()); var responsedata = responsereader.ReadToEnd(); http://pastebin.com/fBTrXZBt – user88 Jan 12 '13 at 14:09
  • this response, I am not able to parse it. that is my problem. – user88 Jan 12 '13 at 14:11
  • Thanks John, So how to get the xml response. – user88 Jan 12 '13 at 15:44
  • How would I know? How would _anyone_ here know? We know nothing about this service! You've wasted over a day, when all you had to do is look at the response! What made you ever think it was XML? The XML you showed earlier - was it even from this same service? – John Saunders Jan 12 '13 at 16:07
  • I am new in web service. If Possible, Please suggest me how to solve this problem and guide me how to learn web service. Thanks again – user88 Jan 12 '13 at 16:08
  • Sorry, you're going to have to ask for help locally. This isn't even the problem you said it is. There may not even be a way to get XML from this service at all, or you're asking the service the wrong way. You need to go back and figure out how the service wants to be spoken to. It's like you're in a Japanese restaurant, ordering in English to someone who thinks you're speaking Japanese, and it's no surprise they give you something you didn't want. – John Saunders Jan 12 '13 at 16:11
  • When I direct pass the URL on browser. I got the xml but when I use StreamReader.I got JSON response not xml. – user88 Jan 12 '13 at 16:15
  • Go read the documentation of the service. Clearly the browser is sending something your program is not. Rather than guess, you should go find out. – John Saunders Jan 12 '13 at 16:16
  • Thanks John, I got it. I am trying to solve this problem.I know, I can do it. – user88 Jan 12 '13 at 16:19
  • 1
    I just told you how to solve it. Go read the documentation. If you insist on doing it the hard way, then use Fiddler or something to watch what the browser does and what your program does. Then please _ask a separate question_ about how to make your program do what the browser does. If you learn how to make XML come back and still have XML parsing questions, then please _ask a separate XML parsing question_. This question is now useless. – John Saunders Jan 12 '13 at 16:21