private DelegateType CreateDelegate(string nameID, string delegationInstant)
        {
            DelegateType del = new DelegateType();

            del.Item = new Uri(nameID, UriKind.RelativeOrAbsolute);
            del.DelegationInstant = XmlConvert.ToDateTime(delegationInstant, XmlDateTimeSerializationMode.Utc);

            return del;
        }
        private DelegationRestrictionType ReadDelegates(XmlReader reader)
        {
            CustomTextTraceSource ts = new CustomTextTraceSource("CommercialVehicleCollisionWebservice.CustomSaml2SecurityTokenHandler.ReadDelegates",
                "MyTraceSource", SourceLevels.Information);

            DelegationRestrictionType delegate2 = null;

            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }
            bool requireDeclaration = false;
            if (reader.IsStartElement("Condition", "urn:oasis:names:tc:SAML:2.0:assertion"))
            {
                reader.Read();
                requireDeclaration = true;
            }
            else if (!reader.IsStartElement("Delegate", "urn:oasis:names:tc:SAML:2.0:conditions:delegation"))
            {
                reader.ReadStartElement("Delegate", "urn:oasis:names:tc:SAML:2.0:conditions:delegation");
            }

            try
            {
                XmlUtil.ValidateXsiType(reader, "DelegationRestrictionType", "urn:oasis:names:tc:SAML:2.0:conditions:delegation", requireDeclaration);

                if (reader.IsEmptyElement)
                {
                    string errorMsg = string.Format("The given element ('{0}','{1}') is empty.", reader.LocalName, reader.NamespaceURI);
                    throw DiagnosticUtil.ThrowHelperXml(reader, errorMsg);
                }

                List<DelegateType> delegates = new List<DelegateType>();

                ts.TraceInformation("Before while (reader.IsStartElement(Delegate, urn:oasis:names:tc:SAML:2.0:conditions:delegation)" );
                ts.TraceInformation("\treader: " + reader.Name);

                while (reader.IsStartElement("Delegate", "urn:oasis:names:tc:SAML:2.0:conditions:delegation"))
                {
                    ts.TraceInformation("Inside while (reader.IsStartElement(Delegate, urn:oasis:names:tc:SAML:2.0:conditions:delegation)");
                    ts.TraceInformation("\treader: " + reader.Name);

                    DelegateType delegateType = new DelegateType();

                    string attribute = reader.GetAttribute("DelegationInstant");
                    if (!string.IsNullOrEmpty(attribute))
                    {
                        delegateType.DelegationInstant = XmlConvert.ToDateTime(attribute, DateTimeFormats.Accepted);
                    }
                                        
                    reader.Read();
                    ts.TraceInformation("After reader.Read()");
                    ts.TraceInformation("\treader: " + reader.Name);

                    if (!reader.IsStartElement("NameID", "urn:oasis:names:tc:SAML:2.0:assertion"))
                    {
                        ts.TraceInformation("Inside !reader.IsStartElement(NameID, urn:oasis:names:tc:SAML:2.0:assertion)");
                        ts.TraceInformation("\treader: " + reader.Name);

                        reader.ReadStartElement("NameID", "urn:oasis:names:tc:SAML:2.0:assertion");
                    }

                    delegateType.Item = ReadSimpleUriElement(reader, UriKind.RelativeOrAbsolute, true);

                    ts.TraceInformation("Delegate NameID: " + delegateType.Item.ToString());


                    delegates.Add(delegateType);

                    reader.ReadEndElement();
                }

                DelegationRestrictionType delegate1 = new DelegationRestrictionType();
                delegate1.Delegate = delegates.ToArray();

                delegate2 = delegate1;
            }
            catch (Exception exception)
            {
                Exception exception2 = DiagnosticUtil.TryWrapReadException(reader, exception);
                if (exception2 == null)
                {
                    throw;
                }

                throw exception2;
            }

            return delegate2;
        }
        private DelegationRestrictionType ReadDelegates(XmlReader reader)
        {
            CustomAdsTextTraceSource ts = new CustomAdsTextTraceSource("IdpAds.OnBehalfOfSaml2SecurityTokenHandler.ReadDelegates",
                "AdsTraceSource", SourceLevels.Information);

            //DelegationRestrictionType delegate2 = null;

            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            bool requireDeclaration = false;
            if (reader.IsStartElement("Condition", "urn:oasis:names:tc:SAML:2.0:assertion"))
            {
                reader.Read();
                requireDeclaration = true;
            }
            else if (!reader.IsStartElement("Delegate", "urn:oasis:names:tc:SAML:2.0:conditions:delegation"))
            {
                reader.ReadStartElement("Delegate", "urn:oasis:names:tc:SAML:2.0:conditions:delegation");
            }
                        
            bool isEmptyElement = reader.IsEmptyElement;
            XmlUtil.ValidateXsiType(reader, "DelegationRestrictionType", "urn:oasis:names:tc:SAML:2.0:conditions:delegation", requireDeclaration);

            if (isEmptyElement)
            {
                string errorMsg = string.Format("The given element ('{0}','{1}') is empty.", reader.LocalName, reader.NamespaceURI);

                throw DiagnosticUtil.ThrowHelperXml(reader, errorMsg);
            }

            List<DelegateType> delegates = new List<DelegateType>();

            while (reader.IsStartElement("Delegate", "urn:oasis:names:tc:SAML:2.0:conditions:delegation"))
            {
                DelegateType delegateType = new DelegateType();

                string attribute = reader.GetAttribute("DelegationInstant");

                if (!string.IsNullOrEmpty(attribute))
                {
                    delegateType.DelegationInstant = XmlConvert.ToDateTime(attribute, DateTimeFormats.Accepted);
                }

                // What does this do?
                reader.Read();

                if (!reader.IsStartElement("NameID", "urn:oasis:names:tc:SAML:2.0:assertion"))
                {
                    reader.ReadStartElement("NameID", "urn:oasis:names:tc:SAML:2.0:assertion");
                }

                // TODO: Investgate the proper way to read this
                delegateType.Item = ReadSimpleUriElement(reader, UriKind.RelativeOrAbsolute, true);
                delegates.Add(delegateType);

                reader.ReadEndElement();
            }

            DelegationRestrictionType delegateRestriction = new DelegationRestrictionType();
            delegateRestriction.Delegate = delegates.ToArray();

            //delegate2 = delegate1;
            return delegateRestriction;
        

            //return delegate2;
        }