System.Xml.Schema.XmlSchemaValidator.AddSchema C# (CSharp) Méthode

AddSchema() public méthode

public AddSchema ( XmlSchema schema ) : void
schema XmlSchema
Résultat void
        public void AddSchema(XmlSchema schema)
        {
            if (schema == null)
            {
                throw new ArgumentNullException(nameof(schema));
            }
            if ((_validationFlags & XmlSchemaValidationFlags.ProcessInlineSchema) == 0)
            { //Do not process schema if processInlineSchema is not set
                return;
            }
            string tns = schema.TargetNamespace;
            if (tns == null)
            {
                tns = string.Empty;
            }
            //Store the previous locations
            Hashtable schemaLocations = _schemaSet.SchemaLocations;
            DictionaryEntry[] oldLocations = new DictionaryEntry[schemaLocations.Count];
            schemaLocations.CopyTo(oldLocations, 0);

            //TODO should i not error if SOM reference same as one in the set or should i assume that inline schema will always be a new SOM ref
            Debug.Assert(_validatedNamespaces != null);
            if (_validatedNamespaces[tns] != null && _schemaSet.FindSchemaByNSAndUrl(schema.BaseUri, tns, oldLocations) == null)
            {
                SendValidationEvent(SR.Sch_ComponentAlreadySeenForNS, tns, XmlSeverityType.Error);
            }
            if (schema.ErrorCount == 0)
            {
                try
                {
                    _schemaSet.Add(schema);
                    RecompileSchemaSet();
                }
                catch (XmlSchemaException e)
                {
                    SendValidationEvent(SR.Sch_CannotLoadSchema, new string[] { schema.BaseUri.ToString(), e.Message }, e);
                }
                for (int i = 0; i < schema.ImportedSchemas.Count; ++i)
                {     //Check for its imports
                    XmlSchema impSchema = (XmlSchema)schema.ImportedSchemas[i];
                    tns = impSchema.TargetNamespace;
                    if (tns == null)
                    {
                        tns = string.Empty;
                    }
                    if (_validatedNamespaces[tns] != null && _schemaSet.FindSchemaByNSAndUrl(impSchema.BaseUri, tns, oldLocations) == null)
                    {
                        SendValidationEvent(SR.Sch_ComponentAlreadySeenForNS, tns, XmlSeverityType.Error);
                        _schemaSet.RemoveRecursive(schema);
                        break;
                    }
                }
            }
        }

Usage Example

Exemple #1
0
        public void InternalSchemaSetShouldUseSeparateXmlResolver()
        {
            CXmlTestResolver res = new CXmlTestResolver();
            CResolverHolder holder = new CResolverHolder();

            res.CalledResolveUri += new XmlTestResolverEventHandler(holder.CallBackResolveUri);
            res.CalledGetEntity += new XmlTestResolverEventHandler(holder.CallBackGetEntity);

            XmlNamespaceManager manager = new XmlNamespaceManager(new NameTable());
            XmlSchemaValidator val = new XmlSchemaValidator(new NameTable(),
                                                            new XmlSchemaSet(){ XmlResolver = new XmlUrlResolver()},
                                                            manager,
                                                            AllFlags);
            val.XmlResolver = res;

            val.Initialize();
            val.AddSchema(XmlSchema.Read(XmlReader.Create(TestData + XSDFILE_VALIDATE_ATTRIBUTE), null)); // this schema has xs:import
            val.ValidateElement("NoAttributesElement", "", null);

            Assert.True(!holder.IsCalledResolveUri);
            Assert.True(!holder.IsCalledGetEntity);

            return;
        }
All Usage Examples Of System.Xml.Schema.XmlSchemaValidator::AddSchema