title: HTML encoding issues - "Â" character showing up instead of "&nbsp;"
body: <p>I've got a legacy app just starting to misbehave, for whatever reason I'm not sure.  It generates a bunch of HTML that gets turned into PDF reports by ActivePDF.</p>
  
  <p>The process works like this:</p>
  
  <ol>
  <li>Pull an HTML template from a DB with tokens in it to be replaced (e.g. "~CompanyName~", "~CustomerName~", etc.)</li>
  <li>Replace the tokens with real data</li>
  <li>Tidy the HTML with a simple regex function that property formats HTML tag attribute values (ensures quotation marks, etc, since ActivePDF's rendering engine hates anything but single quotes around attribute values)</li>
  <li>Send off the HTML to a web service that creates the PDF.</li>
  </ol>
  
  <p>Somewhere in that mess, the non-breaking spaces from the HTML template (the <code>&amp;nbsp;</code>s) are encoding as ISO-8859-1 so that they show up incorrectly as an "Â" character when viewing the document in a browser (FireFox).  ActivePDF pukes on these non-UTF8 characters.</p>
  
  <p>My question: since I don't know where the problem stems from and don't have time to investigate it, is there an easy way to re-encode or find-and-replace the bad characters?  I've tried sending it through this little function I threw together, but it <strike>turns it all into gobbledegook</strike> doesn't change anything.</p>
  
  <pre><code>Private Shared Function ConvertToUTF8(ByVal html As String) As String
      Dim isoEncoding As Encoding = Encoding.GetEncoding("iso-8859-1")
      Dim source As Byte() = isoEncoding.GetBytes(html)
      Return Encoding.UTF8.GetString(Encoding.Convert(isoEncoding, Encoding.UTF8, source))
  End Function
  </code></pre>
  
  <p>Any ideas?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I'm getting by with this for now, though it hardly seems like a good solution:</p>
  
  <pre><code>Private Shared Function ReplaceNonASCIIChars(ByVal html As String) As String
      Return Regex.Replace(html, "[^\u0000-\u007F]", "&amp;nbsp;")
  End Function
  </code></pre>