Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
Hello there
 
I have an application that downloads emails, then parses an attachment.
 
The attachment is an HTML template with various bits of data in it.
 
When I first started writing the parsing code, I used a copy of the HTML file saved directly from an email, and everything was fine.
 
Now, I download an email, and save the file using a function called StoreToFile() in the ActiveUp.Net.Mail library I'm using. Then I open the file with a streamreader to run it through my parsing code.
 
The problem I have is that when I read the file that has been downloaded automatically, the streamreader.readline() brings back a load of randomness. But when looking at both original file and the downloaded file side by side in notepad, they are identical...
 
Example:
First line from original file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
 
First ReadLine from new file (Which looks identical in notepad):
line "
 
<\0!\0d\0o\0c\0t\0y\0p\0e\0>\0" string
 
I thought it maybe encoded as base 64 or something so tried converting it back, but that failed complaining of invalid characters...
 
Does anybody have any ideas what is going on with this? Its driving me up the wall...
 
Any help would be greatly appreciated...
Posted 26-Nov-12 12:47pm
Unsy574
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Every second byte a null? Smells VERY strongly of a 16-bit Unicode encoding. If there isn't a byte order mark at the start of the file, your streamreader may be trying to read it as 8 bit ASCII. I'm no expert in this particular area, but I'm sure there is a way to either get StoreToFile() to write ASCII or (preferably) your streamreader to read Unicode.
 
Have fun,
Peter
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Thanks Peter
 
You are right. For anyone that may come across this problem, here is what I did to get past it:
System.Text.Encoding myEncoding;
myEncoding = Text.Encoding.Unicode;
Using(StreamReader sr = New StreamReader(@"c:\Temp\temp.txt", myEncoding))
{
//My Code
}
 
Thanks again peter.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 397
1 OriginalGriff 350
2 thatraja 205
3 Emre Ataseven 165
4 Shai Vashdi 150
0 Sergey Alexandrovich Kryukov 8,894
1 OriginalGriff 5,200
2 Peter Leow 3,955
3 Maciej Los 3,535
4 Abhinav S 3,208


Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 27 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid