private static TimeSpan DoXmlSizeTestXPathNavigator(int numEntities, XmlSchema schema)
        {
            string xml = CreateXml(numEntities);

            XPathNavigator doc       = CreateXPathNavigator(xml);
            var            schemaSet = new XmlSchemaSet();

            schemaSet.Add(schema);

            DateTime start = DateTime.Now;

            doc.CheckValidity(schemaSet, ValidationEvent);

            return(DateTime.Now - start);
        }
Beispiel #2
0
    public void btnSubmit_Click(object sender, EventArgs e)
    {
        //Load schema used to validate
        XmlSchemaSet schemaSet = new XmlSchemaSet();

        schemaSet.Add(String.Empty, schemaPath);
        schemaSet.Compile();

        //Validate XML using an XPathNavigator's CheckValidity() method
        XPathDocument  doc    = new XPathDocument(xmlPath);
        XPathNavigator nav    = doc.CreateNavigator();
        bool           status = nav.CheckValidity(schemaSet, new ValidationEventHandler(nav_ValidationEventHandler));

        this.lblOutput.Text = (status) ? "Validation Succeeded!" : "Validation Failed!";
    }
Beispiel #3
0
        public void XPathNavigatorMembers(XPathNavigator nav, IXmlNamespaceResolver nsResolver, XPathExpression pathExpression)
        {
            const string constantPath = "/my/xpath/expression";
            string       path         = "variable path";

            // Should not raise for hard-coded paths
            nav.Compile(constantPath);
            nav.Evaluate("xpath-expression");
            nav.Select(constantPath);
            nav.Matches(FixedPath);

            // Should raise for variable paths
            nav.Compile(path);                              // Noncompliant

            nav.Evaluate(path);                             // Noncompliant
            nav.Evaluate(path, nsResolver);                 // Noncompliant
            nav.Evaluate(pathExpression);                   // Compliant - using path expression objects is ok
            nav.Evaluate(pathExpression, null);

            nav.Matches(path);                              // Noncompliant
            nav.Matches(pathExpression);

            // XPathNavigator selection methods
            nav.Select(path);                               // Noncompliant
            nav.Select(path, nsResolver);                   // Noncompliant
            nav.Select(pathExpression);

            nav.SelectSingleNode(path);                     // Noncompliant
            nav.SelectSingleNode(path, nsResolver);         // Noncompliant
            nav.SelectSingleNode(pathExpression);

            nav.SelectAncestors("name", "uri", false);
            nav.SelectChildren("name", "uri");
            nav.SelectDescendants("name", "uri", false);

            nav.AppendChild("newChild");
            nav.AppendChildElement("prefix", "localName", "uri", "value");
            nav.CheckValidity(null, null);

            var nav2 = nav.CreateNavigator();

            nav2.DeleteRange(nav);
        }
Beispiel #4
0
        //Only 1 thread can call this function at any time
        //Since this is an internal class i don't think there is
        //a need for lock for performance reason, but we can put it
        public void Validate(XmlDocument document)
        {
            XPathNavigator navigator = document.CreateNavigator();

            navigator.CheckValidity(schemaSet, new ValidationEventHandler(ValidationCallback));
        }
        /// <summary>
        /// Performs validation against any schemas provided in the document, if any.
        /// If validation is not possible, because no schemas are provided, an InvalidOperationException is thrown.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="InvalidOperationException" />
        public List <Error> Validate(XPathNavigator navigator)
        {
            try
            {
                this.CanValidate = false;

                this.Errors = new List <Error>();

                if (navigator == null)
                {
                    return(null);
                }

                string xsiPrefix = this.XmlNamespaceManager.LookupPrefix("http://www.w3.org/2001/XMLSchema-instance");

                if (string.IsNullOrEmpty(xsiPrefix))
                {
                    this.AddError("The document does not have a schema specified.", XmlSeverityType.Warning);
                    return(this.Errors);
                }

                XmlSchemaSet schemas = new XmlSchemaSet();

                /*
                 * I can't believe I have to do this manually, but, here we go...
                 *
                 * When loading a document with an XmlReader and performing validation, it will automatically
                 * detect schemas specified in the document with @xsi:schemaLocation and @xsi:noNamespaceSchemaLocation attributes.
                 * We get line number and column, but not a reference to the actual offending xml node or xpath navigator.
                 *
                 * When validating an xpath navigator, it ignores these attributes, doesn't give us line number or column,
                 * but does give us the offending xpath navigator.
                 *
                 * */
                foreach (var schemaAttribute in navigator.Select(
                             string.Format("//@{0}:noNamespaceSchemaLocation", xsiPrefix),
                             this.XmlNamespaceManager))
                {
                    XPathNavigator attributeNavigator = schemaAttribute as XPathNavigator;
                    if (attributeNavigator == null)
                    {
                        continue;
                    }

                    string value = attributeNavigator.Value;
                    value = this.ResolveSchemaFileName(value);

                    // add the schema
                    schemas.Add(null, value);
                }
                foreach (var schemaAttribute in navigator.Select(
                             string.Format("//@{0}:schemaLocation", xsiPrefix),
                             this.XmlNamespaceManager))
                {
                    XPathNavigator attributeNavigator = schemaAttribute as XPathNavigator;
                    if (attributeNavigator == null)
                    {
                        continue;
                    }

                    string value = attributeNavigator.Value;

                    List <KeyValuePair <string, string> > namespaceDefs = this.ParseNamespaceDefinitions(value);

                    foreach (var pair in namespaceDefs)
                    {
                        schemas.Add(pair.Key, pair.Value);
                    }
                }

                // validate the document
                navigator.CheckValidity(schemas, this.OnValidationEvent);

                this.CanValidate = true;
            }
            catch (FileNotFoundException ex)
            {
                string message = string.Format(
                    "Cannot find the schema document at '{0}'", ex.FileName);
                this.AddError(message);
            }
            catch (Exception ex)
            {
                this.AddError(ex.Message);
            }

            return(this.Errors);
        }