title: IWebBrowser: How to specify the encoding when loading html from a stream?
body: <p>Using the concepts from the <a href="http://msdn.microsoft.com/en-us/library/aa752047(VS.85).aspx" rel="nofollow noreferrer">sample code provided by Microsoft</a> for loading HTML content into an <a href="http://msdn.microsoft.com/en-us/library/aa752127(VS.85).aspx" rel="nofollow noreferrer">IWebBrowser</a> from an <a href="http://msdn.microsoft.com/en-us/library/aa380034(VS.85).aspx" rel="nofollow noreferrer">IStream</a> using the web browser's <a href="http://msdn.microsoft.com/en-us/library/ms682273(VS.85).aspx" rel="nofollow noreferrer">IPersistStreamInit</a> interface:</p>
  
  <h2>pseudocode:</h2>
  
  <pre><code>void LoadWebBrowserFromStream(IWebBrowser webBrowser, IStream stream)
  {
     IPersistStreamInit persist = webBrowser.Document as IPersistStreamInit;
     persist.Load(stream);
  }
  </code></pre>
  
  <p>How can one specify the encoding of the html inside the IStream? The IStream will contain a series of bytes, but the problem is what do those bytes represent? They could, for example, contain bytes where:</p>
  
  <ul>
  <li>each byte represents a character from the current Windows code-page (e.g. 1252)</li>
  <li>each byte could represent a character from the ISO-8859-1 character set</li>
  <li>the bytes could represent UTF-8 encoded characters</li>
  <li>every 2 bytes could represent a character, using UTF-16 encoding</li>
  </ul>
  
  <p>In my particular case, i am providing the IWebBrowser an IStream that contains a series of double-bytes characters (UTF-16), but the browser (incorrectly) believes that UTF-8 encoding is in effect. This results in garbled characters.</p>
  
  <h2>Workaround solution</h2>
  
  <p>While the question asks how to specify the encoding, in my particular case, with only UTF-16 encoding, there's a simple workaround. Adding the 0xFEFF Byte Order Mark (BOM) indicates that the text is UTF-16 unicode. ie then uses the proper encoding and shows the text properly.</p>
  
  <p>Of course that wouldn't work if the text were encoded, for example with:</p>
  
  <ul>
  <li>UCS-2</li>
  <li>UCS-4</li>
  <li>ISO-10646-UCS-2</li>
  <li>UNICODE-1-1-UTF-8</li>
  <li>UNICODE-2-0-UTF-16</li>
  <li>UNICODE-2-0-UTF-8</li>
  <li>US-ASCII</li>
  <li>ISO-8859-1</li>
  <li>ISO-8859-2</li>
  <li>ISO-8859-3</li>
  <li>ISO-8859-4</li>
  <li>ISO-8859-5</li>
  <li>ISO-8859-6</li>
  <li>ISO-8859-7</li>
  <li>ISO-8859-8</li>
  <li>ISO-8859-9</li>
  <li>WINDOWS-1250</li>
  <li>WINDOWS-1251</li>
  <li>WINDOWS-1252</li>
  <li>WINDOWS-1253</li>
  <li>WINDOWS-1254</li>
  <li>WINDOWS-1255</li>
  <li>WINDOWS-1256</li>
  <li>WINDOWS-1257</li>
  <li>WINDOWS-1258</li>
  </ul>