3

I'm fixing warnings in my program and apparently xmlvalidating reader and xmlschemacollection is obsolete. The problem is, I'm not quite sure how. Here's an attempt at "mimicking" the previous validation function with the new one involving xmlschemaset and xmlreader.create. I first declare a schema, and set it using the targeturi string, then add it to the schemaset while setting up the validation event handler. I think my problem is setting up the readers and the input streams. I knew how to do it with xmlvalidating reader, but that isn't an option if I want to fix these warnings. Here's the code and attempt. During testing, only the new validation xml code was used, the old one was commented out.

            // 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;
    }

Unfortunately, when I run the program and try to validate an invalid xml document, it gives me an error like this: "The 'URNLookup' element is not declared." The URNLookup element is the root element of the xml file. I can always go back to the old validation method, but those warnings scare me.

Any help is appreciated greatly. Thank you in advance! I'll be happy to provide additional information if I missed any.

  • tf.rz (.NET 3.5 SP1, Visual Studio C# 2008)
tf.rz
  • 1,347
  • 6
  • 18
  • 47

1 Answers1

7

I have fixed the issue and it is now working again with no warnings. In the New Validation XML:

            // 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())
                    {
                    }
                }
            }

The ss.add was changed to have a namespace, and the file string. settings.schemas.compile() was added, and the insignificant reorganization of the "using(xmlreader reader.. . . ." was added.

This page helped me a lot: http://msdn.microsoft.com/en-us/library/fe6y1sfe(v=vs.80).aspx It now works.

tf.rz
  • 1,347
  • 6
  • 18
  • 47