示例#1
0
        private FunctionDeclaration ProcessXmlFragment(XmlFragmentContext xmlFragmentContext, FileInfo file)
        {
            // fragment complete, process it
            StringReader stringReader = new StringReader(xmlFragmentContext.XmlFragment.ToString());

            XmlSchemaSet schemas = new XmlSchemaSet();

            schemas.Add(FunctionDeclaration.SchemaUri, FunctionDeclaration.GetSchemaReader());

            XmlReaderSettings settings = new XmlReaderSettings();

            settings.Schemas          = schemas;
            settings.ValidationType   = ValidationType.Schema;
            settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;

            var schemaValidationObject = new SchemaValidationObject(file, xmlFragmentContext.FirstLineNumber, xmlFragmentContext.Indents);

            settings.ValidationEventHandler += schemaValidationObject.CreateValidationHandler();

            XmlReader     xmlReader  = XmlReader.Create(stringReader, settings, file.FullName);
            XmlSerializer serializer = new XmlSerializer(typeof(FunctionDeclaration), FunctionDeclaration.SchemaUri);

            try
            {
                FunctionDeclaration declaration = (FunctionDeclaration)serializer.Deserialize(xmlReader);

                if (schemaValidationObject.HasFailed)
                {
                    return(null);
                }
                else
                {
                    return(declaration);
                }
            }
            catch (InvalidOperationException e)
            {
                XmlException xmlException = e.InnerException as XmlException;
                if (xmlException != null)
                {
                    throw new InputException(
                              InputError.XmlError,
                              file.FullName,
                              xmlException.LineNumber + xmlFragmentContext.FirstLineNumber - 1,
                              xmlException.LinePosition + xmlFragmentContext.Indents[xmlException.LineNumber - 1],
                              xmlException);
                }
                else
                {
                    throw;
                }
            }
        }