try { for(int i=1;i<schemas.Count;i++) { vr.Schemas.Add( null, new XmlTextReader( (Stream)schemas[i] ) ); } vr.ValidationEventHandler += new System.Xml.Schema.ValidationEventHandler(ValidationEventHandler); while( vr.Read() ); return isValid; } finally { vr.Close(); } } /// <summary> /// Contains the validation errors encountered in the last validation operation. /// </summary> public ValidationEventArgs[] Errors { get { return (ValidationEventArgs[])errors.ToArray(typeof(ValidationEventArgs) ); } } /// <summary> /// Helper method to capture the event handlers. /// </summary> /// <param name="sender">The sender of the event.</param> /// <param name="e">The information about the event.</param> private void ValidationEventHandler(object sender, System.Xml.Schema.ValidationEventArgs e) { errors.Add( e ); if ( e.Severity == XmlSeverityType.Warning ) { return; } isValid = false; } #endregion } }
使用: /// <summary> /// Download and validates the manifest using the Uri location. /// </summary> /// <param name="type">The type that represents the manifest class.</param> /// <param name="location">The location of the manifest.</param> /// <param name="schemaResource">The resource where the embedded XSD resides in the assembly.</param> /// <returns>The Manifest instance.</returns> [SecurityPermission(SecurityAction.Demand, SerializationFormatter=true)] private object ValidateAndDeserialize( Type type, Uri location, string schemaResource ) { object result = null; try { string doc = DownloadFile( location ); using ( Stream schema = Assembly.GetExecutingAssembly().GetManifestResourceStream( schemaResource ) ) { SchemaValidator validator = new SchemaValidator( doc, schema ); if ( !validator.Validate() ) { Logger.LogAndThrowException( new ApplicationUpdaterException( Resource.ResourceManager[ Resource.MessageKey.ManifestSchemaError, location, schemaResource ] ) ); } |