473,398 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

XmlSerializer Question

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.ParseBeginTagExpandCharEn tities()
at System.Xml.XmlTextReader.Read()
at System.Xml.XmlReader.ReadElementString()
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationReader1.Read11
_EVariable(Boolean isNullable, Boolean checkType) at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationReader1.Read9_
EmailSpec(Boolean isNullable, Boolean checkType) at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationReader1.Read8_
EmailInfo(Boolean isNullable, Boolean checkType) at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationReader1.Read2_
CRobe(Boolean isNullable, Boolean checkType) at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationReader1.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.
Nov 30 '06 #1
6 5162
I found this article:

http://msdn.microsoft.com/library/de...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?
Nov 30 '06 #2
Hello preport,

From your description, I understand you have an ASP.NET webservice which
transfer some data(retrieved from database) from server to client. However,
it currently report exception when the response data contains some invalid
characters, correct?

Based on my experience, the problem you suffer here is likely due to you
directly expose large blob data(binary or text) through webservice's
paramter or return value. Since webservice request/response data is
encapsulated in XML based SOAP message, if there contains some invalid
byte/chars (such as '<', '>', '&' ...) in the message data, it will cause
error.
For your scenario, I'd like to confirm the following things first:

** how did you define your custom class which contains the database data

** have you already manually encode the data being transfered in
webservice's code?

If convenient you can post some code snippet here so that I can get a clear
view. Also, marking the class as "Serializable" does not help here because
webservice will serialize the class objects through XMLSerialization while
the "Serializable" attribute is used for binary seriailzation.

For this scenario, generally there are two approaches here:

1. In our webservice's code, we can manually use some string or binary
encoding methods to encode the data we would transfer in our custom class's
property, and at client-side, we can decode it. For example, we can use
Convert.ToBase64String to convert binary data into base64 string which is
safe to transfer in webservice. We can also use HttpUtility.HtmlEncode to
make the string/text data safe to be transfer(those particular chars will
be escaped...).

Also, if you do want to send large binary data in webservice, you can
consider using the WSE component (latest version 3.0 for .net framework
2.0).
#Web Services Enhancements (WSE) 3.0 for Microsoft .NET
http://www.microsoft.com/downloads/d...9fd-3a74-43c5-
8ec1-8d789091255d&displaylang=en
2. In .net framework 2.0, it expose the "IXmlSerializable" interface to let
us customize our custom class's XML Serialization process. thus, for your
scenario, you can also consider let your custom class which contains the
database data(contains the invalid chars) and then add the
encoding/escaping code in the IXmlSerialable interface's implementation
methods.

#Customizing XML Serialization in .NET 2.0 Using the IXmlSerializable
Interface
http://www.devx.com/dotnet/Article/29720

#How to: Send and Receive Large Amounts of Data to and from a Web Service
http://msdn.microsoft.com/library/en...4e4-4056-906d-
72504ed8c0df.asp?frame=true

Please feel free to let me know if there is anything I missed or if you
have any further questions.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Dec 1 '06 #3
From your description, I understand you have an ASP.NET webservice which
transfer some data(retrieved from database) from server to client.
However,
it currently report exception when the response data contains some invalid
characters, correct?
Exactly Correct.

** how did you define your custom class which contains the database data
The particular object that blows up has a collection of these:

[Serializable]
public class EVariable
{
private string _name;
private string _value;

public EVariable() { }

public EVariable(string name, string value)
{
this._name = name;
this._value = value;
}

public string Name
{
get { return this._name; }
set { this._name = value; }
}

public string Value
{
get { return this._value; }
set { this._value = value; }
}

}

** have you already manually encode the data being transfered in
webservice's code?
No. I've found that everything gets encoded for me.....most of our objects
contain some kind of HTML that is sent over and when I look at the raw XML
passed to the clients, everything is already encoded.

Infact...the character that is causing the problem gets encoded as: "&#x4;",
In one particular case this is causing the clients to blow up.

While these objects tend to be large, they consist of collections of smaller
objects. What I mean is that I'm not returning an object property populated
by a 'text' or 'blob' DB field. These are small addresses 'varchar(200)'
fields in the DB.

---------

I believe the above answered most of your questions...now....I've been
posting in the csharp newsgroup about using this function to get rid of the
characters in question:

private static string formatXMLString(string n)
{
if (string.IsNullOrEmpty(n)) return n;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
char[] chrs = n.ToCharArray();
char c;
int x, j = chrs.Length;
for (x = 0; x < j; x++)
{
c = chrs[x];
if (c == 0x9 || c == 0xA || c == 0xD ||
(c 0x20 && c < 0xd7ff) ||
(c 0xe000 && c < 0xffd))
{
sb.Append(c);
}
}
return sb.ToString();
}
While it solved the "Clients blowing up" problem, I've lost all the spaces
in these fields.

Any more suggestions?
Dec 1 '06 #4
Thanks for your reply preport,

After some further research, I have found some similar issues in the former
cases and for the invalid character deserialization issue in webservice, it
is occuring when we contains some invalid control character whch is stored
in a .net string variable (class field/property).

So far since we can not customize webservice's internal xmlserializaer(as
the msdn article has mentioned), what we can get are the following
workaround options:
1. Add additional custom code logic to prevent our custom class
properties(string type) from containing those invalid characters. e.g.
manually escapgting those special chars/

2. Use other type other than string to hold such text data. For example,
you can define a propety/field of "XmlElement" type and then store the
original text as inner content of an XmlElment. Or you can use System.Text
namespace's encoding class to encode such text data into byte[] array, and
pass byte array in your webservice.

You can try the above ones to see whether it works for your scenario.
Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.
Dec 4 '06 #5
Thanks...I ended up running this on every property:

private static string formatXMLString(string n)
{
if (string.IsNullOrEmpty(n)) return n;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
char[] chrs = n.ToCharArray();
char c;
int x, j = chrs.Length;
for (x = 0; x < j; x++)
{
c = chrs[x];
if (c == 0x9 || c == 0xA || c == 0xD ||
(c >= 0x20 && c <= 0xd7ff) ||
(c >= 0xe000 && c <= 0xffd))
{
sb.Append(c);
}
}
return sb.ToString();
}

It just strips out that "bad" characters (which is perfectly fine in my
situation). I hope this helps someone else.
Dec 4 '06 #6
Thanks for your followup preport,

Glad that you have worked out a solution.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.
Dec 6 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kevin Conroy | last post by:
I'm making a 6-tiered application that is using Xml to tie the various layers together in a very clean manner. I'm also trying to do this on the 1.1 Framework although I'm willing to switch to the...
4
by: Andy Neilson | last post by:
I've run across a strange behaviour with XmlSerializer that I'm unable to explain. I came across this while trying to use XmlSerializer to deserialize from a the details of a SoapException. This...
16
by: Bob Rock | last post by:
Hello, when serializing an array of elements of a class Classname using XmlSerializer.Serialize() I get an XML like the following: <?xml version="1.0"> <ArrayOfClassname> ....... ..........
2
by: magister | last post by:
Hello I got this working but it is not how I really want it, basically I have an xml file which has a root of <test> and can be filled with 3 different types of <question> elements with different...
3
by: Cindy Liu | last post by:
Hi, I use XmlSerializer to serialize xml string. It escapes xml special charaters, like < and > to < and >. How can I stop XmlSerializer to escape these charaters? Thanks in advance!!! Cindy
12
by: SJD | last post by:
I've just read Christoph Schittko's article on XmlSerializer: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnxmlnet/html/trblshtxsd.asp . . . and very informative it is too....
4
by: BuddyWork | last post by:
Hello, When running the following line of code XmlSerializer serializer = new XmlSerializer(typeof (MyXmlTestClass)); I get the following exception message. An unhandled exception of
1
by: Yewen Tang | last post by:
I have a schema file datamodel.xsd, element "properties" is declared as a type of "baseProperty". The schema file also defines "derivedProperty" is a derived type of "baseProperty". <?xml...
0
by: PSingh | last post by:
Hi, I know this is a frequently asked question but have tried several combinations and cant seem to figure this out. I am serializing my object as follows: XmlSerializer serializer = new...
6
by: Andrew | last post by:
I am using an XmlSerializer to save some settings and I have discovered that when a string is saved containing a cr+nl it is replaced with just a newline when loading back in. I am no expert with...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.