Esempio n. 1
0
        /// <summary>Loads an XML document from a stream</summary>
        /// <param name="schema">Schema to use for validating the XML document</param>
        /// <param name="documentStream">
        ///   Stream from which the XML document will be read
        /// </param>
        /// <returns>The loaded XML document</returns>
        public static XDocument LoadDocument(XmlSchema schema, Stream documentStream)
        {
            XmlReaderSettings settings = new XmlReaderSettings();

            settings.Schemas.Add(schema);

            using (XmlReader reader = XmlReader.Create(documentStream, settings))
            {
                var document = XDocument.Load(reader, LoadOptions.None);

                // Create a schema set because the Validate() method only accepts
                // schemas in a schemaset
                var schemas = new XmlSchemaSet();
                schemas.Add(schema);

                // Perform the validation and report the first validation error
                // encountered to the caller
                var validationEventProcessor = new ValidationEventProcessor();
                document.Validate(schemas, validationEventProcessor.OnValidationEvent);
                if (validationEventProcessor.OccurredException != null)
                {
                    throw validationEventProcessor.OccurredException;
                }

                return(document);
            }
        }
Esempio n. 2
0
        /// <summary>Loads a schema from a text reader</summary>
        /// <param name="schemaReader">Text reader through which the schema can be read</param>
        /// <returns>The loaded schema</returns>
        public static XmlSchema LoadSchema(TextReader schemaReader)
        {
            ValidationEventProcessor eventProcessor = new ValidationEventProcessor();
            XmlSchema schema = XmlSchema.Read(schemaReader, eventProcessor.OnValidationEvent);

            if (eventProcessor.OccurredException != null)
            {
                throw eventProcessor.OccurredException;
            }
            return(schema);
        }
Esempio n. 3
0
        /// <summary>Attempts to load a schema from the provided text reader</summary>
        /// <param name="schemaReader">Reader from which the schema can be read</param>
        /// <param name="schema">Output parameter that will receive the loaded schema</param>
        /// <returns>True if the schema was loaded successfully, otherwise false</returns>
        public static bool TryLoadSchema(TextReader schemaReader, out XmlSchema schema)
        {
            try {
                ValidationEventProcessor eventProcessor = new ValidationEventProcessor();
                schema = XmlSchema.Read(schemaReader, eventProcessor.OnValidationEvent);
                if (eventProcessor.OccurredException == null)
                {
                    return(true);
                }
            }
            catch (Exception) {
                // Munch!
            }

            schema = null;
            return(false);
        }
Esempio n. 4
0
        /// <summary>Loads an XML document from a stream</summary>
        /// <param name="schema">Schema to use for validating the XML document</param>
        /// <param name="documentStream">
        ///   Stream from which the XML document will be read
        /// </param>
        /// <returns>The loaded XML document</returns>
        public static XmlDocument LoadDocument(XmlSchema schema, Stream documentStream)
        {
            XmlReaderSettings settings = new XmlReaderSettings();

            settings.Schemas.Add(schema);

            using (XmlReader reader = XmlReader.Create(documentStream, settings)) {
                XmlDocument document = new XmlDocument();
                document.Schemas.Add(schema);
                document.Load(reader);

                ValidationEventProcessor eventProcessor = new ValidationEventProcessor();
                document.Validate(eventProcessor.OnValidationEvent);
                if (eventProcessor.OccurredException != null)
                {
                    throw eventProcessor.OccurredException;
                }

                return(document);
            }
        }
Esempio n. 5
0
 /// <summary>Loads a schema from a text reader</summary>
 /// <param name="schemaReader">Text reader through which the schema can be read</param>
 /// <returns>The loaded schema</returns>
 public static XmlSchema LoadSchema(TextReader schemaReader) {
   ValidationEventProcessor eventProcessor = new ValidationEventProcessor();
   XmlSchema schema = XmlSchema.Read(schemaReader, eventProcessor.OnValidationEvent);
   if(eventProcessor.OccurredException != null) {
     throw eventProcessor.OccurredException;
   }
   return schema;
 }
Esempio n. 6
0
    /// <summary>Loads an XML document from a stream</summary>
    /// <param name="schema">Schema to use for validating the XML document</param>
    /// <param name="documentStream">
    ///   Stream from which the XML document will be read
    /// </param>
    /// <returns>The loaded XML document</returns>
    public static XDocument LoadDocument(XmlSchema schema, Stream documentStream) {
      XmlReaderSettings settings = new XmlReaderSettings();
      settings.Schemas.Add(schema);

      using (XmlReader reader = XmlReader.Create(documentStream, settings)) {
        var document = XDocument.Load(reader, LoadOptions.None);

        // Create a schema set because the Validate() method only accepts
        // schemas in a schemaset
        var schemas = new XmlSchemaSet();
        schemas.Add(schema);

        // Perform the validation and report the first validation error
        // encountered to the caller
        var validationEventProcessor = new ValidationEventProcessor();
        document.Validate(schemas, validationEventProcessor.OnValidationEvent);
        if (validationEventProcessor.OccurredException != null) {
          throw validationEventProcessor.OccurredException;
        }

        return document;
      }
    }
Esempio n. 7
0
    /// <summary>Loads an XML document from a stream</summary>
    /// <param name="schema">Schema to use for validating the XML document</param>
    /// <param name="documentStream">
    ///   Stream from which the XML document will be read
    /// </param>
    /// <returns>The loaded XML document</returns>
    public static XmlDocument LoadDocument(XmlSchema schema, Stream documentStream) {
      XmlReaderSettings settings = new XmlReaderSettings();
      settings.Schemas.Add(schema);

      using(XmlReader reader = XmlReader.Create(documentStream, settings)) {
        XmlDocument document = new XmlDocument();
        document.Schemas.Add(schema);
        document.Load(reader);

        ValidationEventProcessor eventProcessor = new ValidationEventProcessor();
        document.Validate(eventProcessor.OnValidationEvent);
        if(eventProcessor.OccurredException != null) {
          throw eventProcessor.OccurredException;
        }

        return document;
      }
    }
Esempio n. 8
0
    /// <summary>Attempts to load a schema from the provided text reader</summary>
    /// <param name="schemaReader">Reader from which the schema can be read</param>
    /// <param name="schema">Output parameter that will receive the loaded schema</param>
    /// <returns>True if the schema was loaded successfully, otherwise false</returns>
    public static bool TryLoadSchema(TextReader schemaReader, out XmlSchema schema) {
      try {
        ValidationEventProcessor eventProcessor = new ValidationEventProcessor();
        schema = XmlSchema.Read(schemaReader, eventProcessor.OnValidationEvent);
        if(eventProcessor.OccurredException == null) {
          return true;
        }
      }
      catch(Exception) {
        // Munch!
      }

      schema = null;
      return false;
    }