OCDProgrammer.com

It's Microsoft's World, and I'm just living in it
View Clarence Klopfstein's profile on LinkedIn

Clarence Klopfstein's Facebook profile

This site is under construction...

Categories

New Comments

Referring Sites


Disclaimer

  • This is MY blog. The views represented here are not in relation to anybody else. Please read my full disclaimer for a more complete disclaimer.

'.', hexadecimal value 0x00, is an invalid character. Line 1, position...

October 5, 2008 16:00 by ckincincy

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();

}

Comments

October 16. 2008 00:42

Thanks a lot buddy u saved my time, Im recieving a buffer from other party through socket connection, I converted it to String then I was trying to load it to XMLDocument but always getting this error !! but now everything worked fine.
Thanks alot again

Tariq |

January 20. 2009 09:32


Hex 0x00 is a Null character, and is not displayable nor can it
beused in places of a Null string. If you are trying to retrieve the data
for display (or to manipulate) into a string, you need to capture the fact
and handle accordinally. (I.E. strField = ds("Field"): If ds("Field") is
Nothing Then strField &= "")

Web Design Birmingham |

February 4. 2009 01:42

Its working....
Thanks for saving my time

Prem |

February 13. 2009 13:45

Nice. Thanks

Chris |

April 4. 2009 02:39

Thanks alot. this has helped to save my time

regards
Gopianand S.

Gopianand |

December 17. 2009 13:46

You are my hero!  You have no idea how this issue was killing me.  This was by far the most elegant solution that I found!

mike |

March 31. 2010 14:09

Thanks a lot!

nathan |

April 15. 2010 09:56

Great code! thank  you

Rob |

Comments are closed