bool LoadSchema(XmlSchemaSet set, SchemaResolver resolver, XmlNode ctx, string nsuri, string filename) { try { Uri baseUri = this.baseUri; if (!string.IsNullOrEmpty(ctx.BaseURI)) { baseUri = new Uri(ctx.BaseURI); } Uri resolved; if (baseUri != null) { resolved = new Uri(baseUri, filename); } else { resolved = new Uri(filename, UriKind.RelativeOrAbsolute); } XmlSchema s = resolver.GetEntity(resolved, "", typeof(XmlSchema)) as XmlSchema; if ((s.TargetNamespace + "") != (nsuri + "")) { ReportError(Severity.Warning, SR.TNSMismatch, ctx); } else if (!set.Contains(s)) { set.Add(s); return(true); } } catch (Exception e) { ReportError(Severity.Warning, string.Format(SR.SchemaLoadError, filename, e.Message), ctx); } return(false); }
bool LoadXsiSchemas(XmlDocument doc, XmlSchemaSet set, SchemaResolver resolver) { if (doc.DocumentElement == null) { return(false); } bool result = false; foreach (XmlAttribute a in doc.DocumentElement.Attributes) { if (a.NamespaceURI == "http://www.w3.org/2001/XMLSchema-instance") { if (a.LocalName == "noNamespaceSchemaLocation") { string path = a.Value; if (!string.IsNullOrEmpty(path)) { result = LoadSchema(set, resolver, a, "", a.Value); } } else if (a.LocalName == "schemaLocation") { string[] words = a.Value.Split(new char[] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0, n = words.Length; i + 1 < n; i++) { string nsuri = words[i]; string location = words[++i]; result |= LoadSchema(set, resolver, a, nsuri, location); } } } } return(result); }
private bool LoadSchemasForNamespace(XmlSchemaSet set, SchemaResolver resolver, SchemaCache sc, string nsuri, XmlNode ctx) { bool result = false; if (set.Schemas(nsuri).Count == 0) { CacheEntry ce = sc.FindSchemasByNamespace(nsuri); while (ce != null) { if (!ce.Disabled) { if (!ce.HasUpToDateSchema) { // delay loaded! LoadSchema(set, resolver, ctx, nsuri, ce.Location.AbsoluteUri); } else { set.Add(ce.Schema); } result = true; } ce = ce.Next; } } return(result); }
bool LoadSchemas(XmlDocument doc, XmlSchemaSet set, SchemaResolver resolver) { XmlElement root = doc.DocumentElement; if (root == null) { return(false); } // Give Xsi schemas highest priority. bool result = LoadXsiSchemas(doc, set, resolver); SchemaCache sc = this.cache.SchemaCache; foreach (XmlAttribute a in root.Attributes) { if (a.NamespaceURI == "http://www.w3.org/2000/xmlns/") { string nsuri = a.Value; result |= LoadSchemasForNamespace(set, resolver, sc, nsuri, a); } } if (string.IsNullOrEmpty(root.NamespaceURI)) { result |= LoadSchemasForNamespace(set, resolver, sc, "", root); } return(result); }
public void ValidateContext(XmlCache xcache) { this._cache = xcache; if (string.IsNullOrEmpty(_cache.FileName)) { _baseUri = null; } else { _baseUri = new Uri(new Uri(xcache.FileName), new Uri(".", UriKind.Relative)); } SchemaResolver resolver = xcache.SchemaResolver as SchemaResolver; resolver.Handler = OnValidationEvent; XmlDocument doc = xcache.Document; this._info = new XmlSchemaInfo(); this._nsResolver = new MyXmlNamespaceResolver(doc.NameTable); XmlSchemaSet set = new XmlSchemaSet(); // Make sure the SchemaCache is up to date with document. SchemaCache sc = xcache.SchemaCache; foreach (XmlSchema s in doc.Schemas.Schemas()) { sc.Add(s); } if (LoadSchemas(doc, set, resolver)) { set.ValidationEventHandler += OnValidationEvent; set.Compile(); set.ValidationEventHandler -= OnValidationEvent; } this._validator = new XmlSchemaValidator(doc.NameTable, set, _nsResolver, XmlSchemaValidationFlags.AllowXmlAttributes | XmlSchemaValidationFlags.ProcessIdentityConstraints | XmlSchemaValidationFlags.ProcessInlineSchema); this._validator.ValidationEventHandler += OnValidationEvent; this._validator.XmlResolver = resolver; this._validator.Initialize(); this._nsResolver.Context = doc; ValidateContent(doc); this._nsResolver.Context = doc; this._validator.EndValidation(); }
bool LoadXsiSchemas(XmlDocument doc, XmlSchemaSet set, SchemaResolver resolver) { if (doc.DocumentElement == null) return false; bool result = false; foreach (XmlAttribute a in doc.DocumentElement.Attributes) { if (a.NamespaceURI == "http://www.w3.org/2001/XMLSchema-instance" ) { if (a.LocalName == "noNamespaceSchemaLocation") { string path = a.Value; if (!string.IsNullOrEmpty(path)) { result = LoadSchema(set, resolver, a, "", a.Value); } } else if (a.LocalName == "schemaLocation") { string[] words = a.Value.Split(new char[] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0, n = words.Length; i+1 < n; i++) { string nsuri = words[i]; string location = words[++i]; result |= LoadSchema(set, resolver, a, nsuri, location); } } } } return result; }
private bool LoadSchemasForNamespace(XmlSchemaSet set, SchemaResolver resolver, SchemaCache sc, string nsuri, XmlNode ctx) { bool result = false; if (set.Schemas(nsuri).Count == 0) { CacheEntry ce = sc.FindSchemasByNamespace(nsuri); while (ce != null) { if (!ce.Disabled){ if (!ce.HasUpToDateSchema) { // delay loaded! LoadSchema(set, resolver, ctx, nsuri, ce.Location.AbsoluteUri); } else { set.Add(ce.Schema); } result = true; } ce = ce.Next; } } return result; }
bool LoadSchemas(XmlDocument doc, XmlSchemaSet set, SchemaResolver resolver) { XmlElement root = doc.DocumentElement; if (root == null) return false; // Give Xsi schemas highest priority. bool result = LoadXsiSchemas(doc, set, resolver); SchemaCache sc = this.cache.SchemaCache; foreach (XmlAttribute a in root.Attributes) { if (a.NamespaceURI == "http://www.w3.org/2000/xmlns/") { string nsuri = a.Value; result |= LoadSchemasForNamespace(set, resolver, sc, nsuri, a); } } if (string.IsNullOrEmpty(root.NamespaceURI)){ result |= LoadSchemasForNamespace(set, resolver, sc, "", root); } return result; }
bool LoadSchema(XmlSchemaSet set, SchemaResolver resolver, XmlNode ctx, string nsuri, string filename) { try { Uri baseUri = this.baseUri; if (!string.IsNullOrEmpty(ctx.BaseURI)) { baseUri = new Uri(ctx.BaseURI); } Uri resolved; if (baseUri != null) { resolved = new Uri(baseUri, filename); } else { resolved = new Uri(filename, UriKind.RelativeOrAbsolute); } XmlSchema s = resolver.GetEntity(resolved, "", typeof(XmlSchema)) as XmlSchema; if ((s.TargetNamespace+"") != (nsuri+"")) { ReportError(Severity.Warning, SR.TNSMismatch, ctx); } else if (!set.Contains(s)) { set.Add(s); return true; } } catch (Exception e) { ReportError(Severity.Warning, string.Format(SR.SchemaLoadError, filename, e.Message), ctx); } return false; }