How to use xmlschemaset and xmlreader.create to validate xml using xsd schema

I am fixing warnings in my program and obviously the xmlvalidating reader and xmlschemacollection is out of date. The problem is that I'm not quite sure how to do this. Here's an attempt to "emulate" the previous function of checking new with xmlschemaset and xmlreader.create. First, I declare the schema and set it using the targeturi string, and then add it to the schema when setting up the validation event handler. I think my problem is setting up readers and input streams. I knew how to do this with the xmlvalidating reader, but this is not an option if I want to fix these warnings. Here is the code and try. During testing, only the new xml verification code was used, the old one was commented.

            // New Validation Xml.
            string xsd_file = filename.Substring(0, filename.Length - 3) + "xsd";
            XmlSchema xsd = new XmlSchema();
            xsd.SourceUri = xsd_file;

            XmlSchemaSet ss = new XmlSchemaSet();
            ss.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
            ss.Add(xsd);
            if (ss.Count > 0)
            {
                XmlTextReader r = new XmlTextReader(filename2);
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.ValidationType = ValidationType.Schema;
                settings.Schemas.Add(ss);
                settings.ValidationEventHandler +=new ValidationEventHandler(ValidationCallBack);
                XmlReader reader = XmlReader.Create(filename2, settings);
                while (reader.Read())
                {
                }
                reader.Close();
            }

            // Old Validate XML
            XmlSchemaCollection sc = new XmlSchemaCollection();
            sc.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
            sc.Add(null, xsd_file);
            if (sc.Count > 0)
            {
                XmlTextReader r = new XmlTextReader(filename2);
                XmlValidatingReader v = new XmlValidatingReader(r);
                v.ValidationType = ValidationType.Schema;
                v.Schemas.Add(sc);
                v.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
                while (v.Read())
                {
                }
                v.Close();
            }

    private void ValidationCallBack(object sender, ValidationEventArgs e)
    {
        // If Document Validation Fails
        isvalid = false;
        MessageConsole.Text = "INVALID. Check message and datagridview table.";
        richTextBox1.Text = "The document is invalid: " + e.Message;
    }

, XML-, โ€‹โ€‹: "" URNLookup " ". URNLookup xml. , .

. ! , -.

  • tf.rz(.NET 3.5 SP1, Visual Studio # 2008)
+3
1

, . XML- New Validation:

            // New Validation Xml.
            string xsd_file = filename.Substring(0, filename.Length - 3) + "xsd";
            XmlSchema xsd = new XmlSchema();
            xsd.SourceUri = xsd_file;

            XmlSchemaSet ss = new XmlSchemaSet();
            ss.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
            ss.Add(null, xsd_file);
            if (ss.Count > 0)
            {
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.ValidationType = ValidationType.Schema;
                settings.Schemas.Add(ss);
                settings.Schemas.Compile();
                settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
                XmlTextReader r = new XmlTextReader(filename2);
                using (XmlReader reader = XmlReader.Create(r, settings))
                {
                    while (reader.Read())
                    {
                    }
                }
            }

ss.add . settings.schemas.compile() โ€‹โ€‹ " (xmlreader reader......" ).

: http://msdn.microsoft.com/en-us/library/fe6y1sfe(v=vs.80).aspx .

+7

All Articles