Public Shared Function GetErrorCode(ByVal ex As Exception) As Short Dim errorCode As Short = -1 If (ex.GetType().FullName = "System.Web.Services.Protocols.SoapException") Then Dim sx As System.Web.Services.Protocols.SoapException = ex If (Not sx.Detail Is Nothing AndAlso Not String.IsNullOrEmpty(sx.Detail.InnerXml)) Then Dim message As String = sx.Detail.InnerXml message = "" + message + "" ' The XML message in the exception is not a valid XML. It does not have a ROOT node. Dim doc As New Xml.XmlDocument doc.LoadXml(message) If (doc.GetElementsByTagName("ns1:code").Count > 0 AndAlso Not doc.GetElementsByTagName("ns1:code")(0).FirstChild Is Nothing) Then Short.TryParse(doc.GetElementsByTagName("ns1:code")(0).FirstChild.Value, errorCode) End If End If End If Return errorCode End Function