1

I am getting a invalidoperation exception when I try to Deserialize a webresponse.

In this webresponse I get an XML page which send over https. What my code does is basically send a webrequest to get login-cookies, then another request to get an XML page, afterwards i deserialize that page.

The problem occurs when I try to deserialize, it gives me the following error:

There is an error in XML document (1, 1).

First I looked into my webresponse to see if it has proper data. So I put my webresponse into the streamreader and used readToEnd to convert it into string. Like so:

dim myStringResult as String = myStreamReader.ReadToEnd()

It gave me some letters numbers and blocks. So I think the response I get is the problem. I checked it out with Fiddler and saw that the response needed to be decoded before I could see the content(and yes it did have the proper response I wanted). I went a little deeper into the exception and saw it says

hexadecimal value 0x1F, is an invalid character. Line 1, position 1

I tried looking into the System.Text.Encoding, but couldn't find anything for hexadecimal. Can anyone help me with this?

edit: It's possible that I have to decode it because it is a https response and not because it's hexadecimal encoded.

edit1: I have tried HttpUtility.HtmlDecode unfortunately it didnt change the string.

edit2: example of data i am getting

{� �U�r�0��+(g���(J��ԙ$�k;IomR& �I�N?���/� 6���L�^`���

it doesn't show the data right. It suppose to be mostly squars. Every now and then you see

edit: the header

content-disposition: : attachment; filename="document.xml"
CachingModuleShouldWork: true
Content-Language: en-US

Vary: Accept-Encoding,User-Agent

Content-Length: 382 Content-Type: application/xml

deltu100
  • 581
  • 7
  • 26
  • Can you post an example of the data you're getting back from your http response? – Faster Solutions Nov 21 '12 at 11:01
  • I editted my question. It's really weird that nothing changes. I have done what you asked. Is it because the response is in https and thats why it gave out that 0x1f error? – deltu100 Nov 21 '12 at 11:17
  • What the heck is that? What are you receiving via the HttpResponse? I was expecting and Xml document... That looks like it's some sort of binary output. – Faster Solutions Nov 21 '12 at 15:26
  • exactly, this is my problem - When I used fiddler I can't see it in the textview, but when I right click the response and then use "Decode selected Session" then it will show me the xml. This is what I meant with the response giving me letters, numbers and blocks. I am not entirely sure it is because of https, but fiddler does show if I "Decode" the session that I get to see my xml-document. – deltu100 Nov 21 '12 at 16:02
  • Can you post the content type header for your webresponse (should be in fiddler) – Faster Solutions Nov 22 '12 at 09:36
  • @FasterSolutions added them. – deltu100 Nov 22 '12 at 09:43

2 Answers2

2

0x1f is a Windows control character. It is not valid XML.

You need to decode it first using something like HttpUtility.Decode

So: get your response and read it into a string using a StreamReader then do something like:

StringWriter myWriter = new StringWriter();
         // Decode the encoded string.
         HttpUtility.HtmlDecode(myEncodedString, myWriter);
Faster Solutions
  • 7,005
  • 3
  • 29
  • 45
  • Unfortunately it doesn't work. I put my streamreader into string and did it like you said. It still gives me weird characters and all of that weird stuff in the "myWriter". – deltu100 Nov 21 '12 at 09:28
  • The string just stayed the same. – deltu100 Nov 21 '12 at 09:59
0

If you're decoding content read from the web I recommend you to check this:

HttpUtility.HtmlDecode Method (String)

EDIT: Similar question

hexadecimal value 0x1F, is an invalid character. Line 1, position 1

Community
  • 1
  • 1
Carlos Landeras
  • 11,025
  • 11
  • 56
  • 82
  • Unfortunately it doesn't work, I put my streamreader into string and did it like you said. It still gives me weird characters and all of that weird stuff in the "myWriter". – deltu100 Nov 21 '12 at 09:36