/// <summary>
        ///   Gets the container's attributes, including its metadata and properties, from the response.
        /// </summary>
        /// <param name="response"> The web response. </param>
        /// <returns> The container's attributes. </returns>
        public static BlobContainerAttributes GetAttributes(HttpWebResponse response)
        {
            var containerAttributes = new BlobContainerAttributes
                { Uri = new Uri(response.ResponseUri.GetLeftPart(UriPartial.Path)) };
            var segments = containerAttributes.Uri.Segments;
            containerAttributes.Name = segments[segments.Length - 1];

            var containerProperties = containerAttributes.Properties;
            containerProperties.ETag = response.Headers[HttpResponseHeader.ETag];
            containerProperties.LastModifiedUtc = response.LastModified.ToUniversalTime();

            containerAttributes.Metadata = GetMetadata(response);

            return containerAttributes;
        }
        /// <summary>
        /// Gets the container's attributes, including its metadata and properties, from the response.
        /// </summary>
        /// <param name="response">The web response.</param>
        /// <returns>The container's attributes.</returns>
        public static BlobContainerAttributes GetAttributes(HttpWebResponse response)
        {
            var containerAttributes = new BlobContainerAttributes();

            containerAttributes.Uri = new Uri(response.ResponseUri.GetLeftPart(UriPartial.Path));
            var segments = containerAttributes.Uri.Segments;

            containerAttributes.Name = segments[segments.Length - 1];

            var containerProperties = containerAttributes.Properties;

            containerProperties.ETag            = response.Headers[HttpResponseHeader.ETag];
            containerProperties.LastModifiedUtc = response.LastModified.ToUniversalTime();

            containerAttributes.Metadata = GetMetadata(response);

            return(containerAttributes);
        }
        /// <summary>
        /// Parses the response XML for a container listing operation.
        /// </summary>
        /// <returns>An enumerable collection of <see cref="BlobContainerEntry"/> objects.</returns>
        protected override IEnumerable <BlobContainerEntry> ParseXml()
        {
            bool needToRead = true;

            while (true)
            {
                if (needToRead && !reader.Read())
                {
                    break;
                }

                needToRead = true;

                // Run through the stream until we find what we are looking for.  Retain what we've found.
                if (reader.NodeType == XmlNodeType.Element && !reader.IsEmptyElement)
                {
                    switch (reader.Name)
                    {
                    case Constants.MarkerElement:
                        needToRead            = false;
                        this.marker           = reader.ReadElementContentAsString();
                        this.markerConsumable = true;
                        yield return(null);

                        break;

                    case Constants.NextMarkerElement:
                        needToRead                = false;
                        this.nextMarker           = reader.ReadElementContentAsString();
                        this.nextMarkerConsumable = true;
                        yield return(null);

                        break;

                    case Constants.MaxResultsElement:
                        needToRead                = false;
                        this.maxResults           = reader.ReadElementContentAsInt();
                        this.maxResultsConsumable = true;
                        yield return(null);

                        break;

                    case Constants.PrefixElement:
                        needToRead            = false;
                        this.prefix           = reader.ReadElementContentAsString();
                        this.prefixConsumable = true;
                        yield return(null);

                        break;

                    case Constants.ContainersElement:
                        while (reader.Read())
                        {
                            // We found a container.
                            if (reader.NodeType == XmlNodeType.Element && !reader.IsEmptyElement && reader.Name == Constants.ContainerElement)
                            {
                                BlobContainerAttributes container    = null;
                                Uri                 uri              = null;
                                DateTime?           lastModifiedTime = null;
                                string              etag             = null;
                                string              name             = null;
                                NameValueCollection metadata         = null;

                                // Go until we are out of the container.
                                bool containersNeedToRead = true;
                                while (true)
                                {
                                    if (containersNeedToRead && !reader.Read())
                                    {
                                        break;
                                    }

                                    containersNeedToRead = true;

                                    if (reader.NodeType == XmlNodeType.Element && !reader.IsEmptyElement)
                                    {
                                        switch (reader.Name)
                                        {
                                        case Constants.UrlElement:
                                            string url = reader.ReadElementContentAsString();
                                            containersNeedToRead = false;
                                            Uri.TryCreate(url, UriKind.Absolute, out uri);
                                            break;

                                        case Constants.LastModifiedElement:
                                            lastModifiedTime     = reader.ReadElementContentAsString().ToUTCTime();
                                            containersNeedToRead = false;
                                            break;

                                        case Constants.EtagElement:
                                            etag = reader.ReadElementContentAsString();
                                            containersNeedToRead = false;
                                            break;

                                        case Constants.NameElement:
                                            name = reader.ReadElementContentAsString();
                                            containersNeedToRead = false;
                                            break;

                                        case Constants.MetadataElement:
                                            metadata             = Response.ParseMetadata(reader);
                                            containersNeedToRead = false;
                                            break;
                                        }
                                    }
                                    else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == Constants.ContainerElement)
                                    {
                                        container      = new BlobContainerAttributes();
                                        container.Name = name;
                                        container.Uri  = uri;

                                        if (metadata != null)
                                        {
                                            container.Metadata = metadata;
                                        }

                                        var containerProperties = container.Properties;
                                        containerProperties.ETag = etag;

                                        if (lastModifiedTime != null)
                                        {
                                            containerProperties.LastModifiedUtc = (DateTime)lastModifiedTime;
                                        }

                                        break;
                                    }
                                }

                                yield return(new BlobContainerEntry
                                {
                                    Attributes = container
                                });
                            }
                            else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == Constants.ContainersElement)
                            {
                                this.allObjectsParsed = true;
                                break;
                            }
                        }

                        break;
                    }
                }
            }
        }