开发者

how can I validate dot net application config file(ex, app.exe.config) on console?

is there any tool to 开发者_运维知识库validate configuration file?


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.

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! :-)


Very old question but I had the same question and here is my setup (.net framework 3.5 and up):

  1. I created a console project named 'ConfigurationValidator' :

        static void Main(string[] args)
    {
        try
        {
            string xsdFileName = ConfigurationManager.AppSettings["configXsdPath"];
            string xmlFileName = args[0];
    
            XmlSchemaSet schemas = new XmlSchemaSet();
            schemas.Add(null, xsdFileName);
    
            XDocument doc = XDocument.Load(xmlFileName);
            string validationMessage = string.Empty;
    
            doc.Validate(schemas, (sender, e) => { validationMessage += e.Message + Environment.NewLine; });
    
            if (validationMessage == string.Empty)
            {
                Console.WriteLine("CONFIG FILE IS VALID");
            }
            else
            {
                Console.WriteLine("CONFIG FILE IS INVALID : {0}", validationMessage);
            }
        }
        catch(Exception ex)
        {
            Console.WriteLine("EXCEPTION VALIDATING CONFIG FILE : {0}", ex.Message);
        }
    }
    

and the following app.config :

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <appSettings>
     <add key="configXsdPath" value="C:\Program Files (x86)\Microsoft Visual Studio 11.0\Xml\Schemas\DotNetConfig35.xsd"/>
   </appSettings>
</configuration>
  1. For each project of the solution add post build event command, for example :

    how can I validate dot net application config file(ex, app.exe.config) on console?

when validation succeeds i get the following output :

how can I validate dot net application config file(ex, app.exe.config) on console?

when validation fails i get an output like so:

how can I validate dot net application config file(ex, app.exe.config) on console?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜