locked
Exception while getting a value from Restful webservice. RRS feed

  • Question

  • User1323099333 posted

    Hi ,

    We have a requirement where I need to consume a flag value returned by the webservice. for the same I have written below code .

     

    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);

          WebResponse response = null;

                try

                {

                    response = httpWebRequest.GetResponse();

                    using (Stream stream = response.GetResponseStream())

     {

                        System.Runtime.Serialization.DataContractSerializer dcs = new System.Runtime.Serialization.DataContractSerializer(Type.GetType("System.String"));

                        bool translation = (bool)dcs.ReadObject(stream);

                        Console.WriteLine(translation);

                        Console.ReadKey();

                    }

                 }

    And when I execute , I am seeing below mentioned exception. Could you please help me in understanding the same and suggest what need's to be done.

    Error in line 1 position 77. Expecting element 'string' from namespace 'http://schemas.microsoft.com/2003/10/Serialization/'.. Encountered 'Element'  with name 'ContactType', namespace 'http://url'.

       at System.Runtime.Serialization.DataContractSerializer.InternalReadObject(XmlReaderDelegator xmlReader, Boolean verifyObjectName)
       at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName)
       at System.Runtime.Serialization.XmlObjectSerializer.ReadObject(XmlDictionaryReader reader)
       at System.Runtime.Serialization.XmlObjectSerializer.ReadObject(Stream stream)
       at ConsumeService.Program.Main(String[] args) in C:\Users\A041448\documents\visual studio 2010\Projects\ConsumeService\ConsumeService\Program.cs:line 29

    When I am calling webservice from the browser, below is the response being displayed.

    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
    - <ContactType xmlns="http://URL">
      <ValidationFlag>true</ValidationFlag>
      </ContactType> 

    Tuesday, June 4, 2013 7:20 AM

Answers

  • User260886948 posted

    Error in line 1 position 77. Expecting element 'string' from namespace 'http://schemas.microsoft.com/2003/10/Serialization/'.. Encountered 'Element'  with name 'ContactType', namespace 'http://url'.

    Hi,

    When you serialize something, you serialize an object into some representation such as XML, JSON, etc. If the representation is not binary, the result of serialization can be stored in a string.

    Your code is trying to take a string and deserialize it into a string. That does not make sense.

    The line

    System.Runtime.Serialization.DataContractSerializer dcs = new System.Runtime.Serialization.DataContractSerializer(Type.GetType("System.String"));

    states that the type of the serialized object you are deserializing is type string.

    Please change Type.GetType("System.String") to Type.GetType(WhateverTypeIPreviouslySerialized).

    Hope it can help you.

    Best Regards,
    Amy Peng 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 6, 2013 5:28 AM