/// <summary>
        /// Create a CloudException from a failed response sending XML content.
        /// </summary>
        /// <param name="request">The HTTP request.</param>
        /// <param name="requestContent">The HTTP request content.</param>
        /// <param name="response">The HTTP response.</param>
        /// <param name="responseContent">The HTTP response content.</param>
        /// <param name="innerException">Optional inner exception.</param>
        /// <returns>A CloudException representing the failure.</returns>
        public static new CloudException CreateFromXml(
            HttpRequestMessage request,
            string requestContent,
            HttpResponseMessage response,
            string responseContent,
            Exception innerException = null)
        {
            string code = null;
            string message = null;
            string extendedErrorCode = null;
            string errorMessageFormat = null;
            List<string> errorMessageArguments = null;

            if (responseContent != null)
            {
                try
                {
                    XElement root = XDocument.Parse(responseContent).Root;

                    XElement codeElement = root.Element(XName.Get("Code", "http://schemas.microsoft.com/windowsazure"));
                    if (codeElement != null)
                    {
                        code = codeElement.Value;
                    }

                    XElement messageElement = root.Element(XName.Get("Message", "http://schemas.microsoft.com/windowsazure"));
                    if (messageElement != null)
                    {
                        message = messageElement.Value;
                    }

                    XElement extendedCodeElement = root.Element(XName.Get("ExtendedCode", "http://schemas.microsoft.com/windowsazure"));
                    if (extendedCodeElement != null)
                    {
                        extendedErrorCode = extendedCodeElement.Value;
                    }

                    XElement messageTemplateElement = root.Element(XName.Get("MessageTemplate", "http://schemas.microsoft.com/windowsazure"));
                    if (messageTemplateElement != null)
                    {
                        errorMessageFormat = messageTemplateElement.Value;
                    }

                    XElement parametersElement = root.Element(XName.Get("Parameters", "http://schemas.microsoft.com/windowsazure"));
                    if (parametersElement != null)
                    {
                        errorMessageArguments =
                            parametersElement
                            .Elements(XName.Get("string", "http://schemas.microsoft.com/2003/10/Serialization/Arrays"))
                            .Select(e => e.Value)
                            .ToList();
                    }
                }
                catch
                {
                    // Ignore any and all failures
                }
            }

            // Get the exception message
            string exceptionMessage = null;
            if (code != null || message != null || extendedErrorCode != null)
            {                
                StringBuilder text = new StringBuilder();
                if (code != null)
                {
                    text.Append(code);
                }
                if (extendedErrorCode != null)
                {
                    text.AppendFormat(
                        code != null ? " ({0})" : "{0}",
                        extendedErrorCode);
                }
                if (code != null || extendedErrorCode != null && message != null)
                {
                    text.Append(": ");
                }
                if (message != null)
                {
                    text.Append(message);
                }
                exceptionMessage = text.ToString();
            }
            else
            {
                // Otherwise use the best default that we can come up with
                exceptionMessage =
                    (responseContent != null) ? responseContent :
                    (response != null && response.ReasonPhrase != null) ? response.ReasonPhrase :
                    (response != null) ? response.StatusCode.ToString() :
                    new InvalidOperationException().Message;
            }

            // Create the exception
            WebSiteCloudException exception = new WebSiteCloudException(exceptionMessage, innerException);
            exception.ErrorCode = code;
            exception.ErrorMessage = message;
            exception.Request = CloudHttpRequestErrorInfo.Create(request, requestContent);
            exception.Response = CloudHttpResponseErrorInfo.Create(response, responseContent);
            exception.ExtendedErrorCode = extendedErrorCode;
            exception.ErrorMessageFormat = errorMessageFormat;
            exception.ErrorMessageArguments = errorMessageArguments ?? new List<string>();

            return exception;
        }
        /// <summary>
        /// Create a CloudException from a failed response sending XML content.
        /// </summary>
        /// <param name="request">The HTTP request.</param>
        /// <param name="requestContent">The HTTP request content.</param>
        /// <param name="response">The HTTP response.</param>
        /// <param name="responseContent">The HTTP response content.</param>
        /// <param name="innerException">Optional inner exception.</param>
        /// <returns>A CloudException representing the failure.</returns>
        public static new CloudException CreateFromXml(
            HttpRequestMessage request,
            string requestContent,
            HttpResponseMessage response,
            string responseContent,
            Exception innerException = null)
        {
            string        code                  = null;
            string        message               = null;
            string        extendedErrorCode     = null;
            string        errorMessageFormat    = null;
            List <string> errorMessageArguments = null;

            if (responseContent != null)
            {
                try
                {
                    XElement root = XDocument.Parse(responseContent).Root;

                    XElement codeElement = root.Element(XName.Get("Code", "http://schemas.microsoft.com/windowsazure"));
                    if (codeElement != null)
                    {
                        code = codeElement.Value;
                    }

                    XElement messageElement = root.Element(XName.Get("Message", "http://schemas.microsoft.com/windowsazure"));
                    if (messageElement != null)
                    {
                        message = messageElement.Value;
                    }

                    XElement extendedCodeElement = root.Element(XName.Get("ExtendedCode", "http://schemas.microsoft.com/windowsazure"));
                    if (extendedCodeElement != null)
                    {
                        extendedErrorCode = extendedCodeElement.Value;
                    }

                    XElement messageTemplateElement = root.Element(XName.Get("MessageTemplate", "http://schemas.microsoft.com/windowsazure"));
                    if (messageTemplateElement != null)
                    {
                        errorMessageFormat = messageTemplateElement.Value;
                    }

                    XElement parametersElement = root.Element(XName.Get("Parameters", "http://schemas.microsoft.com/windowsazure"));
                    if (parametersElement != null)
                    {
                        errorMessageArguments =
                            parametersElement
                            .Elements(XName.Get("string", "http://schemas.microsoft.com/2003/10/Serialization/Arrays"))
                            .Select(e => e.Value)
                            .ToList();
                    }
                }
                catch
                {
                    // Ignore any and all failures
                }
            }

            // Get the exception message
            string exceptionMessage = null;

            if (code != null || message != null || extendedErrorCode != null)
            {
                StringBuilder text = new StringBuilder();
                if (code != null)
                {
                    text.Append(code);
                }
                if (extendedErrorCode != null)
                {
                    text.AppendFormat(
                        code != null ? " ({0})" : "{0}",
                        extendedErrorCode);
                }
                if (code != null || extendedErrorCode != null && message != null)
                {
                    text.Append(": ");
                }
                if (message != null)
                {
                    text.Append(message);
                }
                exceptionMessage = text.ToString();
            }
            else
            {
                // Otherwise use the best default that we can come up with
                exceptionMessage =
                    (responseContent != null) ? responseContent :
                    (response != null && response.ReasonPhrase != null) ? response.ReasonPhrase :
                    (response != null) ? response.StatusCode.ToString() :
                    new InvalidOperationException().Message;
            }

            // Create the exception
            WebSiteCloudException exception = new WebSiteCloudException(exceptionMessage, innerException);

            exception.ErrorCode             = code;
            exception.ErrorMessage          = message;
            exception.Request               = CloudHttpRequestErrorInfo.Create(request, requestContent);
            exception.Response              = CloudHttpResponseErrorInfo.Create(response, responseContent);
            exception.ExtendedErrorCode     = extendedErrorCode;
            exception.ErrorMessageFormat    = errorMessageFormat;
            exception.ErrorMessageArguments = errorMessageArguments ?? new List <string>();

            return(exception);
        }