Reading Text

  • Thread starter Thread starter T Cordon
  • Start date Start date
T

T Cordon

I am using a StreamReader to read text from an HTML file and display it as
part of a page in a Label Control. Buy it is not displaying characters as:
ñ, ó, ú, etc.

Please Help.

Thanks
 
This is the code I am using:

Dim sr As StreamReader = File.OpenText(SourceHTMLFile)

Dim input As String

input = sr.ReadToEnd



lblContent.Text = "<BR><BR>" & input

sr.Close()

I know that reads UTF, how can I read it so it displays all the characters?

Thanks
 
I am using a StreamReader to read text from an HTML file and display it as
part of a page in a Label Control. Buy it is not displaying characters as:
ñ, ó, ú, etc.

Make sure you're propely encoding the data for HTML by using the
HttpUtility.HtmlEncode method.
 
The HTML file is created using Word, and by itself it is displayed
correctly.

Guess the problem is reading it or what else can it be?

Thanks
 
The HTML file is created using Word, and by itself it is displayed
correctly.

Are you stripping out the "header" information out of the top of the
HTML file Word creates? Things such as "<HTML>" and "<HEAD>", etc...?
 
No, how could I do that?

This seems to work now, and gets all the chars:

Dim str1 As FileStream = File.OpenRead(SourceHTMLFile)

Dim BA() As Byte = New Byte() {}

Dim input As String

ReDim BA(str1.Length)

Dim x As Integer

For x = 1 To str1.Length

input &= Chr(str1.ReadByte)

Next

lblContent.Text = "<BR><BR>" & input

str1.Close()


Does this seem right?

Thanks Again
 
I believe your going the long way round to get a string from your file.
Here's a simpler example:

Dim sr As StreamReader
Try
sr = New StreamReader(SourceHTMLFile)
lblContent.Text = "<BR><BR>" & sr.ReadToEnd()
Catch
Finally
If Not IsNothing(sr) Then sr.Close()
End Try

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

T Cordon said:
No, how could I do that?

This seems to work now, and gets all the chars:

Dim str1 As FileStream = File.OpenRead(SourceHTMLFile)

Dim BA() As Byte = New Byte() {}

Dim input As String

ReDim BA(str1.Length)

Dim x As Integer

For x = 1 To str1.Length

input &= Chr(str1.ReadByte)

Next

lblContent.Text = "<BR><BR>" & input

str1.Close()


Does this seem right?

Thanks Again
 
No, how could I do that?

This seems to work now, and gets all the chars:

Dim str1 As FileStream = File.OpenRead(SourceHTMLFile)

Dim BA() As Byte = New Byte() {}

Dim input As String

ReDim BA(str1.Length)

Dim x As Integer

For x = 1 To str1.Length

input &= Chr(str1.ReadByte)

Next

lblContent.Text = "<BR><BR>" & input

str1.Close()


Does this seem right?

Your original post showed you using "ReadToEnd" and this one you're
reading a byte at a time and concatenating. Which one are you using?

In any case, the problem could be an encoding issue. If the file is
produced from Word, it may be Unicode. You should ready the file into a
byte array (like you did in the first example) then try using
System.Text.Encoding.Unicode.GetString() on the byte array.

If that doesn't work, try one of the other encoding classes like UTF8.
 
Whoa, now, Patricia! If it is a Word file, it is binary, not Unicode! The
only way to open and read a Word file is to use the Word COM interface.

I assumed that she's reading some type of HTML document, because her
variable is called "SourceHTMLFile". If so, it is pure text, and reading it
into a byte array is not only unnecessary, but may be causing the problem
she's describing, as a byte array is NOT text, but binary.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
My deepest apologies, Patrick, for calling you "Patricia" by accident! :-}

--
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Back
Top