protected bool IsValidXmlDocument(string documentText)
        {
            var failed = false;
            var svec   = new SchemaValidationErrorCollector();

            try
            {
                var settings = new XmlReaderSettings {
                    Schemas = SchemaSet, ValidationType = ValidationType.Schema
                };
                settings.ValidationFlags        |= XmlSchemaValidationFlags.ReportValidationWarnings;
                settings.ValidationEventHandler += svec.ValidationCallBack;

                using (var reader = XmlReader.Create(new StringReader(documentText), settings))
                {
                    while (reader.Read())
                    {
                    }
                }
            }
            catch (Exception)
            {
                failed = true;
            }

            return(svec.ErrorCount <= 0 && failed == false);
        }
        /// <summary>
        ///     Builds the XmlSchemaSet to use for Escher document validation.  This retrieves Xml Schemas that are embedded as resources
        ///     in EntityFramework.dll & Microsoft.Data.Entity.Design.dll.  We use these schemas instead of those installed with VS because
        ///     we know that these schemas will not have been altered by users.
        /// </summary>
        private static XmlSchemaSet BuildEdmxSchemaSet()
        {
            var xmlSchemaSet             = new XmlSchemaSet();
            var validationErrorCollector = new SchemaValidationErrorCollector();

            xmlSchemaSet.ValidationEventHandler += validationErrorCollector.ValidationCallBack;
            xmlSchemaSet.XmlResolver             = new EdmRuntimeSchemaResolver();
            xmlSchemaSet.Add(
                SchemaManager.GetEDMXNamespaceName(EntityFrameworkVersion.Version1),
                EdmxUtils.GetEDMXXsdResource(EntityFrameworkVersion.Version1));
            xmlSchemaSet.Add(
                SchemaManager.GetEDMXNamespaceName(EntityFrameworkVersion.Version2),
                EdmxUtils.GetEDMXXsdResource(EntityFrameworkVersion.Version2));
            xmlSchemaSet.Add(
                SchemaManager.GetEDMXNamespaceName(EntityFrameworkVersion.Version3),
                EdmxUtils.GetEDMXXsdResource(EntityFrameworkVersion.Version3));
            xmlSchemaSet.Compile();
            return(xmlSchemaSet);
        }
Example #3
0
        internal override bool IsXmlValid()
        {
            // If there is a VSXmlModelProvider, we should be able to find a docdata for it.
            // In any other case, it doesn't matter whether there is document data or not.
            var docData = VSHelpers.GetDocData(PackageManager.Package, Uri.LocalPath);

            Debug.Assert(
                !(XmlModelProvider is VSXmlModelProvider) || docData != null, "Using a VSXmlModelProvider but docData is null for Artifact!");

            try
            {
                XmlDocument xmldoc;
                if (docData != null)
                {
                    var textLines = VSHelpers.GetVsTextLinesFromDocData(docData);
                    Debug.Assert(textLines != null, "Failed to get IVSTextLines from docdata");

                    xmldoc = EdmUtils.SafeLoadXmlFromString(VSHelpers.GetTextFromVsTextLines(textLines));
                }
                else
                {
                    // If there is no docdata then attempt to create the XmlDocument from the internal
                    // XLinq tree in the artifact
                    xmldoc = new XmlDocument();
                    xmldoc.Load(XDocument.CreateReader());
                }
                // For the most part, the Edmx schema version of an artifact should be in sync with the schema version
                // that is compatible with the project's target framework; except when the user adds an existing edmx to a project (the version could be different).
                // For all cases, we always want to validate using the XSD's version that matches the artifact's version.
                var documentSchemaVersion = base.SchemaVersion;
                Debug.Assert(
                    EntityFrameworkVersion.IsValidVersion(documentSchemaVersion),
                    "The EF Schema Version is not valid. Value:"
                    + (documentSchemaVersion != null ? documentSchemaVersion.ToString() : "null"));

                // does the XML parse? If not, the load call below will throw
                if (EntityFrameworkVersion.IsValidVersion(documentSchemaVersion))
                {
                    var nsMgr = SchemaManager.GetEdmxNamespaceManager(xmldoc.NameTable, documentSchemaVersion);
                    // Do XSD validation on the document.
                    xmldoc.Schemas = EscherAttributeContentValidator.GetInstance(documentSchemaVersion).EdmxSchemaSet;
                    var svec = new SchemaValidationErrorCollector();

                    // remove runtime specific lines
                    // find the ConceptualModel Schema node
                    RemoveRunTimeNode(xmldoc, "/edmx:Edmx/edmx:Configurations", nsMgr);
                    RemoveRunTimeNode(xmldoc, "/edmx:Edmx/edmx:Runtime/edmx:ConceptualModels", nsMgr);
                    RemoveRunTimeNode(xmldoc, "/edmx:Edmx/edmx:Runtime/edmx:StorageModels", nsMgr);
                    RemoveRunTimeNode(xmldoc, "/edmx:Edmx/edmx:Runtime/edmx:Mappings", nsMgr);

                    xmldoc.Validate(svec.ValidationCallBack);

                    return(svec.ErrorCount == 0);
                }
            }
            catch
            {
            }

            return(false);
        }
 /// <summary>
 ///     Builds the XmlSchemaSet to use for Escher document validation.  This retrieves Xml Schemas that are embedded as resources
 ///     in EntityFramework.dll & Microsoft.Data.Entity.Design.dll.  We use these schemas instead of those installed with VS because
 ///     we know that these schemas will not have been altered by users.
 /// </summary>
 private static XmlSchemaSet BuildEdmxSchemaSet()
 {
     var xmlSchemaSet = new XmlSchemaSet();
     var validationErrorCollector = new SchemaValidationErrorCollector();
     xmlSchemaSet.ValidationEventHandler += validationErrorCollector.ValidationCallBack;
     xmlSchemaSet.XmlResolver = new EdmRuntimeSchemaResolver();
     xmlSchemaSet.Add(
         SchemaManager.GetEDMXNamespaceName(EntityFrameworkVersion.Version1),
         EdmxUtils.GetEDMXXsdResource(EntityFrameworkVersion.Version1));
     xmlSchemaSet.Add(
         SchemaManager.GetEDMXNamespaceName(EntityFrameworkVersion.Version2),
         EdmxUtils.GetEDMXXsdResource(EntityFrameworkVersion.Version2));
     xmlSchemaSet.Add(
         SchemaManager.GetEDMXNamespaceName(EntityFrameworkVersion.Version3),
         EdmxUtils.GetEDMXXsdResource(EntityFrameworkVersion.Version3));
     xmlSchemaSet.Compile();
     return xmlSchemaSet;
 }
        protected bool IsValidXmlDocument(string documentText)
        {
            var failed = false;
            var svec = new SchemaValidationErrorCollector();
            try
            {
                var settings = new XmlReaderSettings { Schemas = SchemaSet, ValidationType = ValidationType.Schema };
                settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
                settings.ValidationEventHandler += svec.ValidationCallBack;

                using (var reader = XmlReader.Create(new StringReader(documentText), settings))
                {
                    while (reader.Read())
                    {
                    }
                }
            }
            catch (Exception)
            {
                failed = true;
            }

            return svec.ErrorCount <= 0 && failed == false;
        }