Invalid character returned from webservice. RRS feed

  • Question

  • Hi,

    We are getting the following error from WSDL generated code that is accessing a webservice, can any one help please?


    System.InvalidOperationException: There is an error in XML document (1, 3267). ---> System.Xml.XmlException: '[1]', hexadecimal value 0x02, is an invalid character. Line 1, position 3267.

       at System.Xml.XmlTextReaderImpl.Throw(Exception e)

       at System.Xml.XmlTextReaderImpl.Throw(String res, String[] args)

       at System.Xml.XmlTextReaderImpl.ThrowInvalidChar(Int32 pos, Char invChar)

       at System.Xml.XmlTextReaderImpl.ParseNumericCharRefInline(Int32 startPos, Boolean expand, BufferBuilder internalSubsetBuilder, Int32& charCount, EntityType& entityType)

       at System.Xml.XmlTextReaderImpl.ParseCharRefInline(Int32 startPos, Int32& charCount, EntityType& entityType)

       at System.Xml.XmlTextReaderImpl.ParseText(Int32& startPos, Int32& endPos, Int32& outOrChars)

       at System.Xml.XmlTextReaderImpl.ParseText()

       at System.Xml.XmlTextReaderImpl.ParseElementContent()

       at System.Xml.XmlTextReaderImpl.Read()

       at System.Xml.XmlTextReader.Read()

       at System.Xml.XmlReader.ReadElementString()

       at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderBookingManagerClientSoap.Read118_BookingComment(Boolean isNullable, Boolean checkType)

       at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderBookingManagerClientSoap.Read355_Booking(Boolean isNullable, Boolean checkType)

       at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderBookingManagerClientSoap.Read497_GetBookingResponse()

       at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer148.Deserialize(XmlSerializationReader reader)

       at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)

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

       at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)

       at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)

       at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)

       at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)

    Thursday, June 12, 2008 3:47 PM


All replies

  • I am also having this issue, and I need to fix it on the client app.  I did find this thread http://it.gps678.com/25/bae58bc1bafeedaa.html, but have not had time to test any of it.

    I know that XML Parsers should throw an exception when they parse illegal characters as this.  But I want to catch the exception, remove all illegal characters and then reparse with minimal impact on overall performance of working responses.  Otherwise customers would not know what the bad data is so that they could correct it.

    I also would like some suggestions on this one if someone has come up with a good solution.  Currently all request/responses are going through my custom soapextension.

    Thanks in advance.

    Friday, June 13, 2008 1:11 AM
  • Thanks for your response, but the link doesn't seem to be relevant to the problems we are having.

    So if there are any other suggestions?

    Friday, June 13, 2008 3:48 PM
  • I just fixed this bug in my project that I am working on.  I have all requests going through a soapextension.  This gives access to the stream before and after serialization.
     See http://msdn.microsoft.com/en-us/library/system.web.services.protocols.soapextension(VS.71).aspx .
      Basically what I did was modify the Copy function from the code from the link above so that it would call CleanInvalidXMLChars.  Before I would get

    InnerException: {"'\b', hexadecimal value 0x08, is an invalid character. Line 1, position 1554."}

    Now it works corrrectly.
    I am going to play with it a little more, but this works.

     private void Copy(Stream from, Stream to) 
                TextReader reader = new StreamReader(from); 
                TextWriter writer = new StreamWriter(to); 
            public static string CleanInvalidXmlChars(string text) 
                // From xml spec valid chars: 
                // #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]     
                // any Unicode character, excluding the surrogate blocks, FFFE, and FFFF. 
                string re = @"[^\x09\x0A\x0D\x20-\xD7FF\xE000-\xFFFD\x10000-x10FFFF]"
                return Regex.Replace(text, re, ""); 

    Tuesday, June 24, 2008 6:35 PM
  • Hi Snedapunk,

    I have the exact same probelm. I need to correct the XMl returned by a webservice to clean up invalid XML characters before it is parsed at clinet end.
    can you please tell me how to write a soapextension at client end, so that I can catch my SOAP reaponse before it is deserialized by "System.Xml.Serialization.XmlSerializer.Deserialize" and please also tell me how I can attach my clent request and response going through this soapextension

    Thursday, September 10, 2009 5:48 PM
  • I got it myself..Thanks
    Thursday, September 10, 2009 8:47 PM