ReadSoapFault method

SoapFaultDetails? ReadSoapFault(
  1. EwsXmlReader reader
)
Reads the SOAP fault. The reader.

Implementation

/* private */
SoapFaultDetails? ReadSoapFault(EwsXmlReader reader) {
  SoapFaultDetails? soapFaultDetails = null;

  try {
    // WCF may not generate an XML declaration.
    reader.Read();
    if (reader.NodeType == XmlNodeType.XmlDeclaration) {
      reader.Read();
    }

    if (!reader.IsStartElement() ||
        (reader.LocalName != XmlElementNames.SOAPEnvelopeElementName)) {
      return soapFaultDetails;
    }

    // Get the namespace URI from the envelope element and use it for the rest of the parsing.
    // If it's not 1.1 or 1.2, we can't continue.
    XmlNamespace soapNamespace =
        EwsUtilities.GetNamespaceFromUri(reader.NamespaceUri);
    if (soapNamespace == XmlNamespace.NotSpecified) {
      return soapFaultDetails;
    }

    reader.Read();

    // Skip SOAP header.
    if (reader.IsStartElementWithNamespace(
        soapNamespace, XmlElementNames.SOAPHeaderElementName)) {
      do {
        reader.Read();
      } while (!reader.IsEndElementWithNamespace(
          soapNamespace, XmlElementNames.SOAPHeaderElementName));

      // Queue up the next read
      reader.Read();
    }

    // Parse the fault element contained within the SOAP body.
    if (reader.IsStartElementWithNamespace(
        soapNamespace, XmlElementNames.SOAPBodyElementName)) {
      do {
        reader.Read();

        // Parse Fault element
        if (reader.IsStartElementWithNamespace(
            soapNamespace, XmlElementNames.SOAPFaultElementName)) {
          soapFaultDetails = SoapFaultDetails.Parse(reader, soapNamespace);
        }
      } while (!reader.IsEndElementWithNamespace(
          soapNamespace, XmlElementNames.SOAPBodyElementName));
    }

    reader.ReadEndElementWithNamespace(
        soapNamespace, XmlElementNames.SOAPEnvelopeElementName);
  } catch (XmlException) {
    // If response doesn't contain a valid SOAP fault, just ignore exception and
    // return null for SOAP fault details.
  }

  return soapFaultDetails;
}