How to check for hexidecimal characters

  • 3/28/2008
  • Author: Brian Pautsch
  • Category: Code Snippets
  • 1306 Views
  • 0 Comments
  • Bookmark and Share

If you haven't come across it yet, hexidecimal characters are not allowed in XML documents and cause problems when trying to display or work with them. When working with 3rd party data in XML (not received via a web service), it's always a good idea to validate the data. If you see an error like "hexidecimal value 0x04, is an invalid character, Line 1 Position 20154755", your problem is the data in the XML document. We recently came across this issue and created a simple method to check for valid characters:

1private bool IsValidString(string input)
2{
3    try
4    {
5        char[] values = input.ToCharArray();
6        foreach (char c in values)
7        {
8            //Get the integral value of the character
9            int value = Convert.ToInt32(c);
10            //Valid character (space -> tilde) see: http://www.asciitable.com
11            if (value < 32 || value > 126)
12                return false;
13        }
14    }
15    catch
16    {
17        return false;
18    }
19    return true;
20}

User Comments

No comments yet. Be the first to leave a comment!

Leave a Comment

Name: E-Mail: URL: Security Code: Type Code: Comments: