XmlSerializer Hexadecimal question

  • Thread starter Thread starter INeedADip
  • Start date Start date
I

INeedADip

I have a webservice that returns data from a database.

Our services (the clients) have been blowing up because of illegal
character problems. Is there anything I can do on the server side to
work around this problem. I'm OK with deleting all the "bad"
characters.

I have a couple DTOs that are marked with [Serializable()] that I
return from the web service after they're populated with data from the
database. Is there some kind of attribute or "easy" way to stip out
illegal characters?

What is weird to me, is that the XmlSerializer serializes it just fine,
the clients are the ones blowing up. Here is the error I'm getting:

---------------------------------

System.InvalidOperationException: There is an error in XML document
(230, 966838). ---> System.Xml.XmlException: '', hexadecimal value
0x04, is an invalid character. Line 230, position 966862.
at System.Xml.XmlScanner.ScanHexEntity()
at System.Xml.XmlTextReader.ParseBeginTagExpandCharEntities()
at System.Xml.XmlTextReader.Read()
at System.Xml.XmlReader.ReadElementString()
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read11
_EVariable(Boolean isNullable, Boolean checkType) at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read9_
EmailSpec(Boolean isNullable, Boolean checkType) at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read8_
EmailInfo(Boolean isNullable, Boolean checkType) at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read2_
CRobe(Boolean isNullable, Boolean checkType) at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read28
_CRobeGetForGuidResponse()

--- End of inner exception stack trace ---

I do populate the object properties by looping through each DataRow in
a DataSet and accessing the dr[column]. Is there a function that will
stip out "illegal" characters?

Any help would be greatly appreciated.
 
I found this article:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnxmlnet/html/trblshtxsd.asp

It says:
You can avoid this problem if you deserialize with an XmlTextReader that has
its Normalization property set to true. Unfortunately, the XmlTextReader
used under the covers by ASP.NET Web services has its Normalization property
set to false; i.e., it will not deserialize SOAP messages containing these
invalid characters.

Does this mean that I'm SOL?
 
So I've now opened up Reflector and started to examine the System.Xml
namespace.
I can see where the parsing is done, but it's a little too advanced for me.
It throws this error:

-----------------------
System.Xml.XmlException: '', hexadecimal value {0}, is an invalid character.
Line {1}, position {2}.
-----------------------

From the code 'this.ThrowInvalidChar(var, var)'.

Here is the code that determines if the character is 'bad':

-----------------------------------------------------------
char ch1 = chArray1[num1];
if ((ch1 >= 0xd800) && (ch1 <= 0xdbff))
{
if ((num1 + 1) == this.ps.charsUsed)
{
goto Label_02B3;
}
num1++;
if ((chArray1[num1] >= 0xdc00) && (chArray1[num1] <= 0xdfff))
{
num1++;
goto Label_0082;
}
}
this.ThrowInvalidChar(num1, ch1);
-----------------------------------------------------------

I've found this identitcal code in the following methods:

Sytem.Xml.XmlTextReaderImpl.ParseAttributeValueChunk()
Sytem.Xml.XmlTextReaderImpl.ParseAttributeValueSlow(...)
Sytem.Xml.XmlTextReaderImpl.ParseCDataOrComment(....)
Sytem.Xml.XmlTextReaderImpl.ParseNumericCharRefInline(....)
Sytem.Xml.XmlTextReaderImpl.ParseIPValue(...)
Sytem.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()
Sytem.Xml.XmlTextReaderImpl.ParseText(....)


I am unsure what the significance of the hex values. It looks to me like
the jist of it is says that:

if ( !((ch1 >= 0xd800) && (ch1 <= 0xdbff)) && !((chArray1[num1+1] >= 0xdc00)
&& (chArray1[num1+1] <= 0xdfff)) )
myXmlData.replace(ch1,'');

I'm really stretching here....Any input would be appreciated.
 
Hello preport,

Regarding on this issue, I have also found your another thread in the
following newsgroup:

Subject: XmlSerializer Question

newsgroup: microsoft.public.dotnet.framework.webservices

I have posted some suggestion and information there. Please feel free to
followup if there is anything we can help.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top