0

I am working on a project developed by a former employee. This project has a class that includes a function designed to accept an XML document and then parse the XML to locate attributes that match properties of the class, then return the class object populated with the property values.

Here is the XSD file, called PTSPodResult.xsd

?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"     xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="PTSPodResult">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="ResultText" type="xs:string" />
        <xs:element name="ReturnCode" type="xs:string" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Here is the class file, called PTSPodResultPartial.vb

Imports System.IO
Imports System.Xml.Serialization

Namespace TrackingObjects.V2.Rev1
    Partial Public Class PTSPodResult
        Shared Function Create(XmlString As String) As PTSPodResult

            Dim sr As StringReader = New StringReader(XmlString.ToString())
            Dim xsw As New XmlSerializer(GetType(PTSPodResult))
            Dim returnResult As New PTSPodResult

            Try
                Dim PTSPodResultLoaded = xsw.Deserialize(sr)
                returnResult = DirectCast(PTSPodResultLoaded, PTSPodResult)

            Catch ex As Exception
                Throw New Exception(XmlString)

            End Try

            Return returnResult

        End Function

        Friend Function ToXml() As String
            Return XsdSerializer.ToXml(Me, Me.GetType, "PTSPodResult")
        End Function
    End Class
End Namespace

When I call the create method, passing it the outerXML from a webresponse, as soon as it hits the first line of the Create function, it returns with an exception. the ex.message value of the exception is the same text as the outerXML string I am passing to the function.

Listed below is a code snippet from the calling class;

Try
    returnValue = TrackingObjects.V2.Rev1.PTSPodResult.Create(Response.OuterXml)
Catch ex As Exception
    RaiseEvent ResponseError(ex.Message)
End Try

Return returnValue

The value of the Response.OuterXml is listed below;

<?xml version="1.0" encoding="UTF-8"?><PTSRRERESULT><ResultText>Your Proof of Delivery record is complete and will be processed shortly.</ResultText><ReturnCode>0</ReturnCode></PTSRRERESULT>

As soon as the Try section is executed, it immediately drops to the Catch section. The ex.Message value is listed below;

<?xml version="1.0" encoding="UTF-8"?><PTSRRERESULT><ResultText>Your Proof of Delivery record is complete and will be processed shortly.</ResultText><ReturnCode>0</ReturnCode></PTSRRERESULT>

As you can see, the excpetion message value is the save text as the response.outerXML I am sending to the TrackingObjects.V2.Rev1.PTSPodResult.Create() function.

My question is, why is the call to the TrackingObjects.V2.Rev1.PTSPodResult.Create() function immediately returning an exception? In the debugger, it is not even stepping into the function as I press F11. It is just returning back to the caller, throwing the exception.

Any ideas?

Thanks,

Dave

Dave Adler
  • 119
  • 1
  • 3
  • 12
  • Change `Throw New Exception(XmlString)` to `Throw New Exception(ex.Message)` for viewing real error message – Fabio Aug 01 '14 at 05:29
  • The new error is "There is an error in XML document (1, 40)." – Dave Adler Aug 01 '14 at 12:56
  • Does that refer to the XML string I am trying to deserialize? That would put it around the tag. The PTSPodResult.vb (the class generated by XSD) has properties for the ResultText and ReturnCode, but nothing for PTSRRERESULT. Could that be it? – Dave Adler Aug 01 '14 at 13:01
  • Well, that wasn't it. I stripped the and tags before sending the XML and it didn't make a difference. – Dave Adler Aug 01 '14 at 13:12
  • Some error you have in your xml as message sad `There is an error in XML document(1,40)`. Check this: [http://stackoverflow.com/questions/4726208/deserialization-error-in-xml-document1-1](http://stackoverflow.com/questions/4726208/deserialization-error-in-xml-document1-1) or [http://stackoverflow.com/questions/16763852/there-is-an-error-in-xml-document-1-2-system-invalidoperationexception-aut](http://stackoverflow.com/questions/16763852/there-is-an-error-in-xml-document-1-2-system-invalidoperationexception-aut) – Fabio Aug 01 '14 at 14:11
  • That link talked about an error at 1,1 not 1,40. Also, the XML I am trying to deserialize is coming from a web response, not a file. – Dave Adler Aug 01 '14 at 15:56
  • 1,40 is line 1, character 40. You are reading `String` value it doesn't metter from where you got it – Fabio Aug 01 '14 at 17:41

0 Answers0