/// <summary> Add the list of possible metadata formats to the OAI-PMH repository information object </summary>
        /// <param name="Repository"> Repository information to add metadata format information to </param>
        /// <returns> TRUE if successful, otherwise FALSE </returns>
        public static bool List_Metadata_Formats(OAI_Repository_Information Repository)
        {
            try
            {
                // prepare the web page we will be asking for
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Repository.Harvested_URL + "?verb=ListMetadataFormats");

                // execute the request
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                // we will read data via the response stream
                Stream resStream = response.GetResponseStream();

                // Was this null?
                if (resStream == null)
                {
                    return(false);
                }

                // Try to read the XML
                XmlTextReader r = new XmlTextReader(resStream);

                while (r.Read())
                {
                    if (r.NodeType == XmlNodeType.Element)
                    {
                        switch (r.Name)
                        {
                        case "metadataPrefix":
                            r.Read();
                            if ((r.NodeType == XmlNodeType.Text) && (r.Value.Trim().Length > 0))
                            {
                                Repository.Add_Metadata_Format(r.Value);
                            }
                            break;
                        }
                    }
                }
            }
            catch
            {
                return(false);
            }

            return(true);
        }
        /// <summary> Adds the list of all the sets to this OAI-PMH repository information object </summary>
        /// <param name="Repository"> Repository information to add set information to </param>
        /// <returns> TRUE if successful, otherwise FALSE </returns>
        public static bool List_Sets(OAI_Repository_Information Repository)
        {
            try
            {
                // prepare the web page we will be asking for
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Repository.Harvested_URL + "?verb=ListSets");

                // execute the request
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                // we will read data via the response stream
                Stream resStream = response.GetResponseStream();

                // Was this null?
                if (resStream == null)
                {
                    return(false);
                }

                // Try to read the XML
                XmlTextReader r = new XmlTextReader(resStream);

                // Sort the list for display purposes
                SortedList <string, KeyValuePair <string, string> > sorter = new SortedList <string, KeyValuePair <string, string> >();

                bool   inSet   = false;
                string setName = String.Empty;
                string setSpec = String.Empty;
                while (r.Read())
                {
                    if (inSet)
                    {
                        if (r.NodeType == XmlNodeType.Element)
                        {
                            switch (r.Name)
                            {
                            case "setSpec":
                                r.Read();
                                if ((r.NodeType == XmlNodeType.Text) && (r.Value.Trim().Length > 0))
                                {
                                    setSpec = r.Value;
                                }
                                break;

                            case "setName":
                                r.Read();
                                if ((r.NodeType == XmlNodeType.Text) && (r.Value.Trim().Length > 0))
                                {
                                    setName = r.Value;
                                }
                                break;
                            }
                        }
                        else if ((r.NodeType == XmlNodeType.EndElement) && (r.Name == "set"))
                        {
                            if ((setSpec.Length > 0) && (setName.Length > 0))
                            {
                                KeyValuePair <string, string> newSet = new KeyValuePair <string, string>(setSpec, setName);
                                sorter.Add(setSpec, newSet);
                            }
                            setSpec = String.Empty;
                            setName = String.Empty;
                            inSet   = false;
                        }
                    }
                    else
                    {
                        if ((r.NodeType == XmlNodeType.Element) && (r.Name == "set"))
                        {
                            inSet   = true;
                            setSpec = String.Empty;
                            setName = String.Empty;
                        }
                    }
                }

                // Now, add each set to the repository
                foreach (string key in sorter.Keys)
                {
                    Repository.Add_Set(sorter[key]);
                }
            }
            catch
            {
                return(false);
            }

            return(true);
        }
        /// <summary> Gets the basic OAI-PMH repository information about a repository, indicated by URL </summary>
        /// <param name="OAI_URL"> URL for the OAI-PMH repository to query </param>
        /// <returns> Information about the repository, from an Identify OAI-PMH request </returns>
        public static OAI_Repository_Information Identify(string OAI_URL)
        {
            // Create the return object
            OAI_Repository_Information returnValue = new OAI_Repository_Information(OAI_URL);

            try
            {
                // prepare the web page we will be asking for
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(OAI_URL + "?verb=Identify");

                // execute the request
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                // we will read data via the response stream
                Stream resStream = response.GetResponseStream();

                // Was it null?
                if (resStream == null)
                {
                    returnValue.Is_Valid = false;
                    return(returnValue);
                }


                // Try to read the XML
                XmlTextReader r = new XmlTextReader(resStream);

                bool inOaiResponse = false;
                while (r.Read())
                {
                    if (inOaiResponse)
                    {
                        if (r.NodeType == XmlNodeType.Element)
                        {
                            switch (r.Name)
                            {
                            case "repositoryName":
                                r.Read();
                                if ((r.NodeType == XmlNodeType.Text) && (r.Value.Trim().Length > 0))
                                {
                                    returnValue.Name = r.Value;
                                }
                                break;

                            case "adminEmail":
                                r.Read();
                                if ((r.NodeType == XmlNodeType.Text) && (r.Value.Trim().Length > 0))
                                {
                                    returnValue.Admin_Email = r.Value;
                                }
                                break;

                            case "baseURL":
                                r.Read();
                                if ((r.NodeType == XmlNodeType.Text) && (r.Value.Trim().Length > 0))
                                {
                                    returnValue.Base_URL = r.Value;
                                }
                                break;

                            case "protocolVersion":
                                r.Read();
                                if ((r.NodeType == XmlNodeType.Text) && (r.Value.Trim().Length > 0))
                                {
                                    returnValue.Protocol_Version = r.Value;
                                }
                                break;

                            case "earliestDatestamp":
                                r.Read();
                                if ((r.NodeType == XmlNodeType.Text) && (r.Value.Trim().Length > 0))
                                {
                                    returnValue.Earliest_Date_Stamp = r.Value;
                                }
                                break;

                            case "deletedRecord":
                                r.Read();
                                if ((r.NodeType == XmlNodeType.Text) && (r.Value.Trim().Length > 0))
                                {
                                    returnValue.Deleted_Record = r.Value;
                                }
                                break;

                            case "granularity":
                                r.Read();
                                if ((r.NodeType == XmlNodeType.Text) && (r.Value.Trim().Length > 0))
                                {
                                    returnValue.Granularity = r.Value;
                                }
                                break;

                            case "repositoryIdentifier":
                                r.Read();
                                if ((r.NodeType == XmlNodeType.Text) && (r.Value.Trim().Length > 0))
                                {
                                    returnValue.Repository_Identifier = r.Value;
                                }
                                break;

                            case "delimiter":
                                r.Read();
                                if ((r.NodeType == XmlNodeType.Text) && (r.Value.Trim().Length > 0))
                                {
                                    returnValue.Delimiter = r.Value;
                                }
                                break;

                            case "sampleIdentifier":
                                r.Read();
                                if ((r.NodeType == XmlNodeType.Text) && (r.Value.Trim().Length > 0))
                                {
                                    returnValue.Sample_Identifier = r.Value;
                                }
                                break;
                            }
                        }
                    }
                    else
                    {
                        if ((r.NodeType == XmlNodeType.Element) && (r.Name == "OAI-PMH"))
                        {
                            inOaiResponse = true;
                        }
                    }
                }
            }
            catch
            {
                returnValue.Is_Valid = false;
            }

            return(returnValue);
        }
        /// <summary> Adds the list of all the sets to this OAI-PMH repository information object </summary>
        /// <param name="Repository"> Repository information to add set information to </param>
        /// <returns> TRUE if successful, otherwise FALSE </returns>
        public static bool List_Sets(OAI_Repository_Information Repository)
        {
            try
            {
                // prepare the web page we will be asking for
                HttpWebRequest request = (HttpWebRequest) WebRequest.Create(Repository.Harvested_URL + "?verb=ListSets");

                // execute the request
                HttpWebResponse response = (HttpWebResponse) request.GetResponse();

                // we will read data via the response stream
                Stream resStream = response.GetResponseStream();

                // Was this null?
                if (resStream == null)
                    return false;

                // Try to read the XML
                XmlTextReader r = new XmlTextReader(resStream);

                // Sort the list for display purposes
                SortedList<string, KeyValuePair<string, string>> sorter = new SortedList<string, KeyValuePair<string, string>>();

                bool inSet = false;
                string setName = String.Empty;
                string setSpec = String.Empty;
                while (r.Read())
                {
                    if (inSet)
                    {
                        if (r.NodeType == XmlNodeType.Element)
                        {
                            switch (r.Name)
                            {
                                case "setSpec":
                                    r.Read();
                                    if ((r.NodeType == XmlNodeType.Text) && (r.Value.Trim().Length > 0))
                                    {
                                        setSpec = r.Value;
                                    }
                                    break;

                                case "setName":
                                    r.Read();
                                    if ((r.NodeType == XmlNodeType.Text) && (r.Value.Trim().Length > 0))
                                    {
                                        setName = r.Value;
                                    }
                                    break;
                            }
                        }
                        else if ((r.NodeType == XmlNodeType.EndElement) && (r.Name == "set"))
                        {
                            if ((setSpec.Length > 0) && (setName.Length > 0))
                            {
                                KeyValuePair<string, string> newSet = new KeyValuePair<string, string>(setSpec, setName);
                                sorter.Add(setSpec, newSet);
                            }
                            setSpec = String.Empty;
                            setName = String.Empty;
                            inSet = false;
                        }
                    }
                    else
                    {
                        if ((r.NodeType == XmlNodeType.Element) && (r.Name == "set"))
                        {
                            inSet = true;
                            setSpec = String.Empty;
                            setName = String.Empty;
                        }
                    }
                }

                // Now, add each set to the repository
                foreach (string key in sorter.Keys)
                {
                    Repository.Add_Set(sorter[key]);
                }
            }
            catch
            {
                return false;
            }

            return true;
        }
        /// <summary> Gets the basic OAI-PMH repository information about a repository, indicated by URL </summary>
        /// <param name="OAI_URL"> URL for the OAI-PMH repository to query </param>
        /// <returns> Information about the repository, from an Identify OAI-PMH request </returns>
        public static OAI_Repository_Information Identify(string OAI_URL)
        {
            // Create the return object
            OAI_Repository_Information returnValue = new OAI_Repository_Information(OAI_URL);

            try
            {
                // prepare the web page we will be asking for
                HttpWebRequest request = (HttpWebRequest) WebRequest.Create(OAI_URL + "?verb=Identify");

                // execute the request
                HttpWebResponse response = (HttpWebResponse) request.GetResponse();

                // we will read data via the response stream
                Stream resStream = response.GetResponseStream();

                // Was it null?
                if (resStream == null)
                {
                    returnValue.Is_Valid = false;
                    return returnValue;
                }

                // Try to read the XML
                XmlTextReader r = new XmlTextReader(resStream);

                bool inOaiResponse = false;
                while (r.Read())
                {
                    if (inOaiResponse)
                    {
                        if (r.NodeType == XmlNodeType.Element)
                        {
                            switch (r.Name)
                            {
                                case "repositoryName":
                                    r.Read();
                                    if ((r.NodeType == XmlNodeType.Text) && (r.Value.Trim().Length > 0))
                                    {
                                        returnValue.Name = r.Value;
                                    }
                                    break;

                                case "adminEmail":
                                    r.Read();
                                    if ((r.NodeType == XmlNodeType.Text) && (r.Value.Trim().Length > 0))
                                    {
                                        returnValue.Admin_Email = r.Value;
                                    }
                                    break;

                                case "baseURL":
                                    r.Read();
                                    if ((r.NodeType == XmlNodeType.Text) && (r.Value.Trim().Length > 0))
                                    {
                                        returnValue.Base_URL = r.Value;
                                    }
                                    break;

                                case "protocolVersion":
                                    r.Read();
                                    if ((r.NodeType == XmlNodeType.Text) && (r.Value.Trim().Length > 0))
                                    {
                                        returnValue.Protocol_Version = r.Value;
                                    }
                                    break;

                                case "earliestDatestamp":
                                    r.Read();
                                    if ((r.NodeType == XmlNodeType.Text) && (r.Value.Trim().Length > 0))
                                    {
                                        returnValue.Earliest_Date_Stamp = r.Value;
                                    }
                                    break;

                                case "deletedRecord":
                                    r.Read();
                                    if ((r.NodeType == XmlNodeType.Text) && (r.Value.Trim().Length > 0))
                                    {
                                        returnValue.Deleted_Record = r.Value;
                                    }
                                    break;

                                case "granularity":
                                    r.Read();
                                    if ((r.NodeType == XmlNodeType.Text) && (r.Value.Trim().Length > 0))
                                    {
                                        returnValue.Granularity = r.Value;
                                    }
                                    break;

                                case "repositoryIdentifier":
                                    r.Read();
                                    if ((r.NodeType == XmlNodeType.Text) && (r.Value.Trim().Length > 0))
                                    {
                                        returnValue.Repository_Identifier = r.Value;
                                    }
                                    break;

                                case "delimiter":
                                    r.Read();
                                    if ((r.NodeType == XmlNodeType.Text) && (r.Value.Trim().Length > 0))
                                    {
                                        returnValue.Delimiter = r.Value;
                                    }
                                    break;

                                case "sampleIdentifier":
                                    r.Read();
                                    if ((r.NodeType == XmlNodeType.Text) && (r.Value.Trim().Length > 0))
                                    {
                                        returnValue.Sample_Identifier = r.Value;
                                    }
                                    break;
                            }
                        }
                    }
                    else
                    {
                        if ((r.NodeType == XmlNodeType.Element) && (r.Name == "OAI-PMH"))
                            inOaiResponse = true;
                    }
                }
            }
            catch
            {
                returnValue.Is_Valid = false;
            }

            return returnValue;
        }
        /// <summary> Add the list of possible metadata formats to the OAI-PMH repository information object </summary>
        /// <param name="Repository"> Repository information to add metadata format information to </param>
        /// <returns> TRUE if successful, otherwise FALSE </returns>
        public static bool List_Metadata_Formats(OAI_Repository_Information Repository)
        {
            try
            {
                // prepare the web page we will be asking for
                HttpWebRequest request = (HttpWebRequest) WebRequest.Create(Repository.Harvested_URL + "?verb=ListMetadataFormats");

                // execute the request
                HttpWebResponse response = (HttpWebResponse) request.GetResponse();

                // we will read data via the response stream
                Stream resStream = response.GetResponseStream();

                // Was this null?
                if (resStream == null)
                    return false;

                // Try to read the XML
                XmlTextReader r = new XmlTextReader(resStream);

                while (r.Read())
                {
                    if (r.NodeType == XmlNodeType.Element)
                    {
                        switch (r.Name)
                        {
                            case "metadataPrefix":
                                r.Read();
                                if ((r.NodeType == XmlNodeType.Text) && (r.Value.Trim().Length > 0))
                                {
                                    Repository.Add_Metadata_Format(r.Value);
                                }
                                break;
                        }
                    }
                }
            }
            catch
            {
                return false;
            }

            return true;
        }