Working with Null Characters in C# / .Net

Sat, Dec 20, 2008

Tech Tips

Here is one method to manipulate Null characters in C#:

To Search a String for a Null character:

mystring.Contains(Convert.ToChar(0x0).ToString() );
//The hexidecimal 0x0 is the null character

To Replace all Null Characters in a String:

mystring.Replace(Convert.ToChar(0x0).ToString(), "");

Why Do I Need This?
It might help. I needed it when I got a nasty error message in a data layer that was using asp.net web services and soap to retrieve the data:
“There is an error in XML document hexadecimal value 0×00, is an invalid character”
Basically what happened is that a null character was pasted into a SQL Server database via an Access front end. When my asp.net app was pulling the data via a SOAP data layer, the null character can not be used in a XML document with UTF encoding. Which is the transport method of this particualr service. So the quick hack was to strip out the null character after the data pull but before the soap transfer back to the client.

Bookmark and Share
,

One Response to “Working with Null Characters in C# / .Net”

  1. Ashu Says:

    Grt work dude it helped me alot…thanx

    Reply


Leave a Reply