如何在控制台上验证dot net应用程序配置文件(例如,app.exe.config)? [英] how can I validate dot net application config file(ex, app.exe.config) on console?

查看:24
本文介绍了如何在控制台上验证dot net应用程序配置文件(例如,app.exe.config)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么工具可以验证配置文件?

is there any tool to validate configuration file?

推荐答案

好吧,基本上,您的应用是验证器-如果配置文件无效,则启动时会出现异常.除此之外,我不知道对app.config文件有任何现成的验证支持.

Well, basically your app is the validator - if the config file is not valid, you'll get an exception when starting up. Other than that, I'm not aware of any out-of-the-box validation support for app.config files.

在目录 C:\ Program Files \ Microsoft Visual Studio 9.0 \ Xml \ Schemas 中,您会找到一些名为 DotNetConfig.xsd/DotNetConfig20.xsd 的文件-这些是Microsoft提供的XML模式文件,您可以轻松地使用它们来验证您可能具有的其他任何配置文件的有效性.

In your directory C:\Program Files\Microsoft Visual Studio 9.0\Xml\Schemas, you'll find some files called DotNetConfig.xsd / DotNetConfig20.xsd - those are Microsoft-supplied XML schema files, which you could easily use to validate any other config files you might have for validity.

以编程方式验证配置的基本结构如下:

The basic structure for programmatically validating your configs would be something like this:

using(StreamReader xsdReader = new StreamReader(xsdFileName))
{
    XmlSchema Schema = new XmlSchema();
    Schema = XmlSchema.Read(xsdReader, new ValidationEventHandler(XSDValidationEventHandler));

    XmlReaderSettings ReaderSettings = new XmlReaderSettings();    
    ReaderSettings.ValidationType = ValidationType.Schema;                
    ReaderSettings.Schemas.Add(Schema);   

    ReaderSettings.ValidationEventHandler += new ValidationEventHandler(XMLValidationEventHandler);

    using(XmlTextReader xmlReader = new XmlTextReader(xmlFileName))
    {
        XmlReader objXmlReader = XmlReader.Create(xmlReader, ReaderSettings);

        while (objXmlReader.Read())
        {   }
    }
}
Console.WriteLine("Successful validation completed!");

您现在需要做的是为验证中出现问题时引发的事件提供事件处理程序,仅此而已!:-)

What you need to do now is supply event handlers for those event that get raised when something in the validation goes wrong - that's about it! :-)

这篇关于如何在控制台上验证dot net应用程序配置文件(例如,app.exe.config)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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