private XmlReaderSettings CreateXmlReaderSettings() { var readerSettings = CreateEdmStandardXmlReaderSettings(); // add flags readerSettings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings; readerSettings.ValidationEventHandler += OnSchemaValidationEvent; readerSettings.ValidationType = ValidationType.Schema; var schemaSet = SomSchemaSetHelper.GetSchemaSet(DataModel); // Do not use readerSetting.Schemas.Add(schemaSet) // you must use the line below for this to work in // a multithread environment readerSettings.Schemas = schemaSet; return(readerSettings); }
/// <summary> /// Populate the schema object from a schema /// </summary> /// <param name="sourceReader"> TextReader containing the schema xml definition </param> /// <param name="source"> Uri containing path to a schema file (may be null) </param> /// <returns> list of errors </returns> private IList <EdmSchemaError> InternalParse(XmlReader sourceReader, string sourceLocation) { DebugCheck.NotNull(sourceReader); // these need to be set before any calls to AddError are made. Schema = this; Location = sourceLocation; try { // to make life simpler, we skip down to the first/root element, unless we're // already there if (sourceReader.NodeType != XmlNodeType.Element) { while (sourceReader.Read() && sourceReader.NodeType != XmlNodeType.Element) { } } GetPositionInfo(sourceReader); var expectedNamespaces = SomSchemaSetHelper.GetPrimarySchemaNamespaces(DataModel); // the root element needs to be either TDL or Schema in our namespace if (sourceReader.EOF) { if (sourceLocation != null) { AddError(ErrorCode.EmptyFile, EdmSchemaErrorSeverity.Error, Strings.EmptyFile(sourceLocation)); } else { AddError(ErrorCode.EmptyFile, EdmSchemaErrorSeverity.Error, Strings.EmptySchemaTextReader); } } else if (!expectedNamespaces.Contains(sourceReader.NamespaceURI)) { Func <object, object, object, string> messageFormat = Strings.UnexpectedRootElement; if (string.IsNullOrEmpty(sourceReader.NamespaceURI)) { messageFormat = Strings.UnexpectedRootElementNoNamespace; } var expectedNamespacesString = Helper.GetCommaDelimitedString(expectedNamespaces); AddError( ErrorCode.UnexpectedXmlElement, EdmSchemaErrorSeverity.Error, messageFormat(sourceReader.NamespaceURI, sourceReader.LocalName, expectedNamespacesString)); } else { SchemaXmlNamespace = sourceReader.NamespaceURI; if (DataModel == SchemaDataModelOption.EntityDataModel) { if (SchemaXmlNamespace == XmlConstants.ModelNamespace_1) { SchemaVersion = XmlConstants.EdmVersionForV1; } else if (SchemaXmlNamespace == XmlConstants.ModelNamespace_1_1) { SchemaVersion = XmlConstants.EdmVersionForV1_1; } else if (SchemaXmlNamespace == XmlConstants.ModelNamespace_2) { SchemaVersion = XmlConstants.EdmVersionForV2; } else { Debug.Assert(SchemaXmlNamespace == XmlConstants.ModelNamespace_3, "Unknown namespace in CSDL"); SchemaVersion = XmlConstants.EdmVersionForV3; } } else if (DataModel == SchemaDataModelOption.ProviderDataModel) { if (SchemaXmlNamespace == XmlConstants.TargetNamespace_1) { SchemaVersion = XmlConstants.StoreVersionForV1; } else if (SchemaXmlNamespace == XmlConstants.TargetNamespace_2) { SchemaVersion = XmlConstants.StoreVersionForV2; } else { Debug.Assert(SchemaXmlNamespace == XmlConstants.TargetNamespace_3, "Unknown namespace in SSDL"); SchemaVersion = XmlConstants.StoreVersionForV3; } } switch (sourceReader.LocalName) { case "Schema": case "ProviderManifest": HandleTopLevelSchemaElement(sourceReader); // this forces the reader to look beyond this top // level element, and complain if there is another one. sourceReader.Read(); break; default: AddError( ErrorCode.UnexpectedXmlElement, EdmSchemaErrorSeverity.Error, Strings.UnexpectedRootElement(sourceReader.NamespaceURI, sourceReader.LocalName, SchemaXmlNamespace)); break; } } } catch (InvalidOperationException ex) { AddError(ErrorCode.InternalError, EdmSchemaErrorSeverity.Error, ex.Message); } catch (UnauthorizedAccessException ex) { AddError(ErrorCode.UnauthorizedAccessException, EdmSchemaErrorSeverity.Error, sourceReader, ex); } catch (IOException ex) { AddError(ErrorCode.IOException, EdmSchemaErrorSeverity.Error, sourceReader, ex); } catch (SecurityException ex) { AddError(ErrorCode.SecurityError, EdmSchemaErrorSeverity.Error, sourceReader, ex); } catch (XmlException ex) { AddError(ErrorCode.XmlError, EdmSchemaErrorSeverity.Error, sourceReader, ex); } return(ResetErrors()); }