Definitely sounds like it could be an encoding problem.  You could try
replacing this function of yours with
System.IO.File.ReadAllText<http://msdn.microsoft.com/en-us/library/system.io.file.readalltext.aspx>,
which should attempt to automatically detect the file encoding for you.

Alternatively, open the file in a decent text editor (like Notepad++), which
will be able to tell you the encoding of the file.

Also, have you checked if an exception is being thrown? I notice you are
catching it and printing it to trace output. Any leads there?

Joe.




On Mon, Nov 15, 2010 at 10:32 PM, Jano Petras <[email protected]> wrote:

> Hi Anthony,
>
> It seems related to encoding mark (see
> http://en.wikipedia.org/wiki/Byte_order_mark).
>
> Just throwing few ideas into the air here as haven't really tried this
> myself - but maybe you could try using StreamReader constructor overload
> that accepts encoding so you could pass Encoding.Unicode to see if it solves
> the issue.  If not, reading the file as stream of bytes and then use the
> Encoding converter to convert it to string or strip off the byte order mark
> at the beginning and convert then?
>
> There is a similar topic discussed here
> http://stackoverflow.com/questions/1835430/byte-order-mark-screws-up-file-reading-in-javabut
>  uses Java to explain the issue and resolution.
>
>
> Cheers,
> jano
>
>
>
>
> On 15 November 2010 10:10, Anthony <[email protected]> wrote:
>
>>  I have been using this function for years with no problem..now when i
>> try to open a file(contains XML)..it returns an empty string even though it
>> has text.  I have noticed that the XML  fie has some null charcaters at
>> the begining..any suggestion?
>>
>> Public Shared Function GetFileContents(ByVal FullPath As String) As
>> String
>>
>>        Dim stream As FileStream = Nothing
>>
>>        Dim strContents As String = String.Empty
>>
>>         Try
>>
>>             stream = File.Open(FullPath, FileMode.Open, FileAccess.Read)
>>
>>             Dim Tin As System.IO.StreamReader = 
>> NewSystem.IO.StreamReader(stream)
>>
>>             strContents = Tin.ReadToEnd()
>>
>>             Tin.Close()
>>
>>             stream.Close()
>>
>>             Return strContents
>>
>>         Catch ex As Exception
>>
>>             Trace.WriteLine(ex.Message)
>>
>>             Return ""
>>
>>         Finally
>>
>>             If Not stream Is Nothing Then
>>
>>                 stream.Close()
>>
>>             End If
>>
>>         End Try
>>
>>     End Function
>>
>>  *****Is your website being 
>> IntelliXperienced?*<http://www.intellixperience.com/signup.aspx>
>> | www.yougoingmyway.com ?
>> regards
>> Anthony (*12QWERNB*)
>>
>> Is your website being IntelliXperienced?
>>
>>
>>
>>
>

Reply via email to