System.Net.Policy.ClientAccessPolicy.FromStream C# (CSharp) Method

FromStream() static public method

static public FromStream ( Stream stream ) : ICrossDomainPolicy
stream Stream
return ICrossDomainPolicy
		static public ICrossDomainPolicy FromStream (Stream stream)
		{
			ClientAccessPolicy cap = new ClientAccessPolicy ();

			// Silverlight accepts whitespaces before the XML - which is invalid XML
			StreamReader sr = new StreamReader (stream);
			while (Char.IsWhiteSpace ((char) sr.Peek ()))
				sr.Read ();

			XmlReaderSettings policy_settings = new XmlReaderSettings ();
			policy_settings.DtdProcessing = DtdProcessing.Ignore;
			using (XmlReader reader = XmlReader.Create (sr, policy_settings)) {
				reader.MoveToContent ();
				if (reader.IsEmptyElement) {
					reader.Skip ();
					return null;
				}
				reader.ReadStartElement ("access-policy", String.Empty);
				for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
					if (reader.NodeType != XmlNodeType.Element)
						throw new XmlException (String.Format ("Unexpected access-policy content: {0}", reader.NodeType));

					if ((reader.LocalName != "cross-domain-access") || reader.IsEmptyElement) {
						reader.Skip ();
						continue;
					}

					reader.ReadStartElement ("cross-domain-access", String.Empty);
					for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
						if (reader.NodeType != XmlNodeType.Element)
							throw new XmlException (String.Format ("Unexpected access-policy content: {0}", reader.NodeType));

						if ((reader.Name != "policy") || reader.IsEmptyElement) {
							reader.Skip ();
							continue;
						}

						ReadPolicyElement (reader, cap);
					}
					reader.ReadEndElement ();
				}
				reader.ReadEndElement ();
			}
			return cap;
		}

Usage Example

コード例 #1
0
        public static ICrossDomainPolicy BuildSilverlightPolicy(HttpWebResponse response)
        {
            // return null if no Silverlight policy was found, since we offer a second chance with a flash policy
            if ((response.StatusCode != HttpStatusCode.OK) || !CheckContentType(response.ContentType))
            {
                return(null);
            }

            ICrossDomainPolicy policy = null;

            try
            {
                policy = ClientAccessPolicy.FromStream(response.GetResponseStream());
                if (policy != null)
                {
                    AddPolicy(response.ResponseUri, policy);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("CrossDomainAccessManager caught an exception while reading {0}: {1}",
                                                response.ResponseUri, ex));
                // and ignore.
            }
            return(policy);
        }
All Usage Examples Of System.Net.Policy.ClientAccessPolicy::FromStream