I know I can validate xml against a schema using a callback method like the following, but is there a way that I can do it synchronously instead of event driven?
One way I thought of would be to set a class member boolean flag IsValidated=false then
call xml.Validate(ValidationEventHandler). The event handler would set IsValidated=true once it's finished. In the mean time,  do a loop checking until the flag is set to true then continue.
This is for .Net 3.5.
    public bool ValidateSchema(string xmlPath, string xsdPath)
    {
        XmlDocument xml = new XmlDocument();
        xml.Load(xmlPath);
        xml.Schemas.Add(null, xsdPath);
        xml.Validate(ValidationEventHandler); 
    }
Ok, I have done a test and it appears that xml.validate actually waits until the callback has completed before new code is executed.
In the following example, the MessageBox.Show("After Validate"); always happens after the execution of myValidationEventHandler.
I also stepped through the code to verify this.
So I guess that makes my question a non issue.
// load etc.
...
xmlValidate(myValidationEventHandler);
MessageBox.Show("After Validate");
    private void myValidationEventHandler(object sender, ValidationEventArgs e)
    {
        for (double i = 0; i < 100000; i++)
        {
            textBox1.Text = i.ToString();
            Application.DoEvents();
        }
    // do stuff with e
    }
                You can specify null for the ValidationEventHandler to have the Validate method throw an exception.
    public bool ValidateSchema(string xmlPath, string xsdPath)
    {
        XmlDocument xml = new XmlDocument();
        xml.Load(xmlPath);
        xml.Schemas.Add(null, xsdPath);
        try
        {
            xml.Validate(null);
        }
        catch (XmlSchemaValidationException)
        {
            return false;
        }
        return true;
    }
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With