Sometimes you have to take on a massive data import endeavor involving SQL
scripts, DTS, ODBC connections to old databases. God knows what kind of string
data you end up with. Now, you say to yourself, "self...I want to create some
Web Services" to share my data with other systems.
Try this one on for size...Let's say you query your database and you end up
with a string value that contains a hexadecimal tab value. In C#, that would
look like "\v" as an escape character. Since you are a smart Web Service
developer, you take a contract-first approach. You create your XSDs for your
messages (param and return values) and created classes using xsd /c at the
command line. And then it hits you...
Unhandled Exception:
System.InvalidOperationException: There is an error in XML
document (1, 286). ---> System.Xml.XmlException: '♂', hexadecimal value 0x0B, is
an invalid character. Line 1, position 340.
So, what's the best way to handle this? Clean the data in the database?
Filter the data coming out of the database before you put it on the wire from
the Web service? Use a CDATA tag?
Here's an example of the problem....
//Client app
class
App
{
[STAThread]
static void
Main(string[] args)
{
localhost.Service1 service = new
localhost.Service1();
string s =
service.GetStringWithEscapeCharacter();
Console.WriteLine(s);
}
}
//Web Service
[WebMethodAttribute]
public
string GetStringWithEscapeCharacter()
{
return "This is a test. \v Boom!";
}