Last week we upgraded our company from Sybase to SQL Server 2005. All of a sudden one of our web applications started throwing this error:
'.', hexadecimal value 0x00, is an invalid character. Line 1, position 198128.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Xml.XmlException: '.', hexadecimal value 0x00, is an invalid character. Line 1, position 198128.
This was happening when I was calling a .NET web service via an ASPX page. The web service call returned a serialized object:
For understanding, I have a class similar to this:
Model.Customer cust = new Customer();
cust.Id = 1;
cust.Name = "CK IN CINCY";
Now for the code that gave me this error (ie the ASPX code behind):
Model.Customer cust = new Customer();
WebSvc.GetCustomer(1, out cust);
This would blow up.
The web service looked like this:
[WebMethod]
public int GetCustomer(int Id, out Model.Customer cust)
{
// Fill cust object from database
return 1;
}
The problem was the data in the database contained null characters. This is to understood as different than null records. A null record is self explanitory and can be deal with inside the sql with an isnull call. The problem was that the record existed but somehow has a null character. CK IN [NULL CHARACTER] CINCY. In this case, isnull won't solve the problem. So after about five hours of searching I found this post. Which contained this function, which did the trick for me. Hopefully this saves you 5 hours of your time :-).
/// <summary>
/// Removes control characters and other non-UTF-8 characters
/// </summary>
/// <param name="inString">The string to process</param>
/// <returns>A string with no control characters or entities above 0x00FD</returns>
public static string RemoveTroublesomeCharacters(string inString)
{
if (inString == null) return null;
StringBuilder newString = new StringBuilder();
char ch;
for (int i = 0; i < inString.Length; i++)
{
ch = inString[i];
// remove any characters outside the valid UTF-8 range as well as all control characters
// except tabs and new lines
if ((ch < 0x00FD && ch > 0x001F) || ch == '\t' || ch == '\n' || ch == '\r')
{
newString.Append(ch);
}
}
return newString.ToString();
}