title: C#/Java Validate Unicode characters in Xamarin Android
body: <p>I’m sending XML to a 3rd party web service. Certain characters in the XML causes the web service to throw an exception, an example is:</p>
  
  <blockquote>
    <p>System.ServiceModel.FaultException was unhandled HResult=-2146233087<br>
    Message=System.Web.Services.Protocols.SoapException: Server was unable
    to process request. ---> System.Text.EncoderFallbackException: Unable
    to translate Unicode character \u0193 at index 281 to specified code
    page.</p>
  </blockquote>
  
  <p>The exception is not thrown in certain circumstances:</p>
  
  <ol>
  <li>If the character is within the first 0-255 characters of the Unicode character table, apart from some in the 128-160 range, or</li>
  <li>For some specific Unicode characters above the 255 range, such as “ƒ” (402). I notice most of these are marked as “in WGL4” on this <a href="https://en.wikipedia.org/wiki/List_of_Unicode_characters" rel="nofollow noreferrer">page</a>. However, some which are marked as “in WGL4” do actually cause the exception.</li>
  </ol>
  
  <p>We are seeing this issue when users type into a text box, or paste into the text box from a Word document or email (and the text is then sent to the web service).</p>
  
  <p>I’d like to restrict which characters can be entered into the text box. As a start I have this rudimentary test:</p>
  
  <pre><code>if ((chr &gt; 255) || (!XmlConvert.IsXmlChar(chr)))
  {
    // Restrict character
  }
  </code></pre>
  
  <p>However, that doesn’t permit some of the valid characters above 255. How can I add a condition to allow for these, without resorting to hardcoding a long list of possible values e.g. is there an existing method I could use?</p>