同步XML Schema验证? .NET 3.5 [英] Synchronous XML Schema Validation? .NET 3.5

查看:159
本文介绍了同步XML Schema验证? .NET 3.5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以使用如下的回调方法验证XML对一个架构,但有什么办法,我能做到同步,而不是事件驱动的?



我想到了一个办法是设置IsValidated = A类成员布尔标志为假,则结果
调用xml.Validate(的ValidationEventHandler)。该事件处理程序将设置IsValidated = TRUE,一旦它的完成。与此同时,做一个循环检查,直到标志设置为true,那么继续。



这是对于.NET 3.5。

 公共BOOL ValidateSchema(字符串xmlpath中,字符串xsdPath)
{
XmlDocument的XML =新的XmlDocument();
与XML.load(xmlpath中);

xml.Schemas.Add(NULL,xsdPath);

xml.Validate(的ValidationEventHandler);
}



好吧,我曾做过一个试验,看来,xml.validate实际上等待直至回调已被执行新的代码之前完成



在下面的实例中,MessageBox.Show(经过验证); myValidationEventHandler执行后总是会发生的。



我也通过代码加强,以验证这一点。



所以我想这大概使我的问题非问题。

  //负荷等


XMLVALIDATE(myValidationEventHandler);

MessageBox.Show(经过验证);


私人无效myValidationEventHandler(对象发件人,ValidationEventArgs E)
{
为(双I = 0; I< 100000;我++)
{
textBox1.Text = i.ToString();
Application.DoEvents();
}

//做的东西随e
}


解决方案

您可以指定为null的ValidationEventHandler有验证方法抛出异常。

 公共BOOL ValidateSchema(字符串xmlpath中,字符串xsdPath)
{
XmlDocument的XML =新的XmlDocument();
与XML.load(xmlpath中);

xml.Schemas.Add(NULL,xsdPath);


{
xml.Validate(NULL);
}
赶上(XmlSchemaValidationException)
{
返回FALSE;
}
返回真;
}


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

这篇关于同步XML Schema验证? .NET 3.5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆