Streamwriter removes french characters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rob

    #1

    Streamwriter removes french characters


    Hi,

    I have a small VB.Net program that reads in an HTML file using a
    FileStream (this file was created by MS Word "Save as HTML" feature),
    uses regular expressions to remove all unwanted code and then re-writes
    the file.

    It works fine but when I execute this on a french web page...the
    StreamWriter removes all of the french characters. Here's a piece of the
    code:

    Dim filename As String = txtFilename.Tex t.ToString

    Dim sr As StreamReader

    sr = File.OpenText(f ilename)

    Dim textstream As String = sr.ReadToEnd()

    sr.Close()

    Dim newtext As String

    newtext = CleanHTML(texts tream)

    Dim fs As New FileStream(Outp utFilename, FileMode.Create ,
    FileAccess.Writ e)

    Dim sw As New StreamWriter(fs )

    ' I've also tried:
    'Dim sw as New StreamWriter(fs , System.Text.Enc oding.UTF8)

    sw.WriteLine(ne wtext)

    sw.Close()



    I'm kinda new to .Net Development...d oes anyone see what's wrong here?



    Thanks



    *** Sent via Developersdex http://www.developersdex.com ***
  • Spam Catcher

    #2
    Re: Streamwriter removes french characters

    Rob <robert@hotmail .comwrote in news:OAplhegnHH A.4552
    @TK2MSFTNGP04.p hx.gbl:
    newtext = CleanHTML(texts tream)
    What does this function do? Is it stripping out french characters?

    When you "clean" HTML... what are you doing? Have you tried
    Server.HTMLEnco de instead?

    Comment

    • Rob

      #3
      Re: Streamwriter removes french characters

      When I run the CleanHTML function...I remove a lot of garbage that comes
      with saving a Word document as HTML. I remove all the styles, span tags
      and a lot of other tags that are not needed.

      When I clean up a french webpage, the StreamReader or StreamWriter
      doesn't seen to understand french characters and strips them out but I
      want to preserve them.

      Another thing I've tried is, rather than using this line:
      Dim sr As StreamReader
      sr = File.OpenText(f ilename)

      I've replaced it with this
      Dim sr As New StreamReader(fi lename, System.Text.Enc oding.Default)

      but this returns strange result as well.



      *** Sent via Developersdex http://www.developersdex.com ***

      Comment

      • Rob

        #4
        Re: Streamwriter removes french characters

        I got it.

        I added System.text.Enc oding.Default to my StreamWriter as well as my
        StreamReader and that fixed it.

        Thanks



        *** Sent via Developersdex http://www.developersdex.com ***

        Comment

        • Herfried K. Wagner [MVP]

          #5
          Re: Streamwriter removes french characters

          "Rob" <robert@hotmail .comschrieb:
          I have a small VB.Net program that reads in an HTML file using a
          FileStream (this file was created by MS Word "Save as HTML" feature),
          uses regular expressions to remove all unwanted code and then re-writes
          the file.
          >
          It works fine but when I execute this on a french web page...the
          StreamWriter removes all of the french characters. Here's a piece of the
          code:
          >
          Dim filename As String = txtFilename.Tex t.ToString
          Remove the '.ToString' as 'txtFilename.Te xt' is already of type 'String'.
          Dim sr As StreamReader
          >
          sr = File.OpenText(f ilename)
          >
          Dim textstream As String = sr.ReadToEnd()
          Note that the 'StreamReader' object is initialized for the UTF-8 encoding by
          default. If your file has been encoded using a different encoding, such as
          Windows' default ANSI codepage, then pass 'System.Text.En coding.Default' in
          the parameter accepting an encoding of the 'StreamReader'' s constructor.

          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

          Comment

          Working...