I have what seems to be a simple problem, but the explanation may be a bit
complex.  In summary, I get an exception deserializing xml to an object when
done in a windows application trying to build the object to pass to a web
service method, but the same code with the same xml works fine inside the
web service (i.e. when a web service method takes the xml as a string and
deserializes it to the target object).

The signature of the web service method in question looks like:

public ResponseObj SubmitData(SubmissionObj obj)

The Windows Application has a web reference to the web service, called
AFRSServiceWS.  The app takes a string in xml-format and attempts to
deserialize it to the SubmissionObj as follows:

=============Code Begins=================
AFRSServiceWS.SubmissionObj submission = new AFRSServiceWS.SubmissionObj();

try
        {
                XmlSerializer serializer = new
XmlSerializer(typeof(AFRSServiceWS.SubmissionObj));
                string sub = txtSubmission.Text;  // this is xml to
deserialize
                byte[] bytes = Encoding.ASCII.GetBytes(sub);
                MemoryStream stream = new MemoryStream(bytes);
                submission = (AFRSServiceWS.AFRSSubmission)
serializer.Deserialize(stream);
        }
        catch (Exception ex)
        {
                MessageBox.Show("Deserialization of submission failed with "
+ ex.Message);
        }
===============Code Ends=============

The xml starts with:

<?xml version="1.0"?><AFRS_Submission
xmlns:xsd="http://www.w3.org/2001/XMLSchema";
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";> ...

The exception I receive is always:

System.InvalidOperationException: There is an error in XML document (1, 23).
---> System.InvalidOperationException: <AFRS_Submission xmlns=''> was not
expected....

NOTE THAT THE EXACT SAME CODE WITH THE EXACT SAME XML DESERIALIZES
SUCCESSFULLY WITHIN THE WEB SERVICE ITSELF  (AN ALTERNATE WEB METHOD TAKES
THE XML AS A STRING AND DOES THE DESERIALIZATION).

The only difference in the code is that the alternate method uses the
SubmissionObj directly instead of having to reference it through the web
service as the windows app has to do.

Also note that if I supply a namespace in instancing the XmlSerializer, such
as:

XmlSerializer serializer = new
XmlSerializer(typeof(AFRSServiceWS.SubmissionObj),"http://myurl.gov/afrsserv
ice");

...and supply the same namespace in the xml, such as in:

<?xml version="1.0"?><AFRS_Submission
xmlns:xsd="http://www.w3.org/2001/XMLSchema";
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xlmns="http://myurl.gov/afrsservice"; password=""> ...

it doesn't make the slightest difference in the outcome, except that it
names the namespace, as in:

System.InvalidOperationException: There is an error in XML document (1, 23).
---> System.InvalidOperationException: <AFRS_Submission
xmlns='http://myurl.gov/afrsservice'> was not expected....

I've tried numerous combinations to no avail, including leaving off the
xmlns:xsd and :xsi elements.

It seems like the critical difference between working and non-working
deserialization is doing so using an object defined on the web service.  And
defining the identical object in the win app and trying to cast it to the
web service object doesn't work, either.

I have searched archives, msdn, and a number of other lists with no luck.  I
hope someone here can tell me what is going on, and what I can do to fix
this.

===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com
Some .NET courses you may be interested in:

NEW! Guerrilla ASP.NET, 17 May 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnetls

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to