/// <summary>
        /// Detects the payload kind(s) from the message stream.
        /// </summary>
        /// <param name="messageInfo">The context information for the message.</param>
        /// <param name="settings">Configuration settings of the OData reader.</param>
        /// <returns>An enumerable of zero or one payload kinds depending on whether the metadata payload kind was detected or not.</returns>
        private static IEnumerable <ODataPayloadKind> DetectPayloadKindImplementation(
            ODataMessageInfo messageInfo,
            ODataMessageReaderSettings settings)
        {
            var detectionInfo = new ODataPayloadKindDetectionInfo(messageInfo, settings);

            try
            {
                using (var reader = ODataMetadataReaderUtils.CreateXmlReader(
                           messageInfo.MessageStream, detectionInfo.GetEncoding(), detectionInfo.MessageReaderSettings))
                {
                    if (reader.TryReadToNextElement() &&
                        string.CompareOrdinal(EdmConstants.EdmxName, reader.LocalName) == 0 &&
                        reader.NamespaceURI == EdmConstants.EdmxOasisNamespace)
                    {
                        return(new ODataPayloadKind[] { ODataPayloadKind.MetadataDocument });
                    }
                }
            }
            catch (XmlException)
            {
                // If we are not able to read the payload as XML it is not a metadata document.
                // Return no detected payload kind below.
            }

            return(Enumerable.Empty <ODataPayloadKind>());
        }
        /// <summary>Constructor.</summary>
        /// <param name="messageInfo">The context information for the message.</param>
        /// <param name="messageReaderSettings">Configuration settings of the OData reader.</param>
        public ODataMetadataInputContext(
            ODataMessageInfo messageInfo,
            ODataMessageReaderSettings messageReaderSettings)
            : base(ODataFormat.Metadata, messageInfo, messageReaderSettings)
        {
            Debug.Assert(messageInfo.MessageStream != null, "messageInfo.MessageStream != null");

            try
            {
                // Which encoding do we use when reading XML payloads
                this.baseXmlReader = ODataMetadataReaderUtils.CreateXmlReader(messageInfo.MessageStream, messageInfo.Encoding, messageReaderSettings);

                // We use the buffering reader here only for in-stream error detection (not for buffering).
                this.xmlReader = new BufferingXmlReader(
                    this.baseXmlReader,
                    /*parentXmlReader*/ null,
                    messageReaderSettings.BaseUri,
                    /*disableXmlBase*/ false,
                    messageReaderSettings.MessageQuotas.MaxNestingDepth);
            }
            catch (Exception e)
            {
                // Dispose the message stream if we failed to create the input context.
                if (ExceptionUtils.IsCatchableExceptionType(e))
                {
                    messageInfo.MessageStream.Dispose();
                }

                throw;
            }
        }