How can I check the dot net application configuration file (ex, app.exe.config) in the console?

Is there any tool to check the configuration file?

+6
source share
2 answers

Well, basically your application is a validator - if the configuration file is invalid, you will get an exception at startup. Other than that, I don't know any ready-made support for checking app.config files.

In your C:\Program Files\Microsoft Visual Studio 9.0\Xml\Schemas you will find several files called DotNetConfig.xsd / DotNetConfig20.xsd - these are XML schema files supplied by Microsoft that you can easily use to check any other files configurations that may be available for reality.

The basic structure for programmatically checking your configurations will look 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!"); 

Now you need to make event event handlers for those events that occur when something in the scan goes wrong - about it !:-)

+6
source share

A very old question, but I had the same question, and here is my setup (.net framework 3.5 and higher):

  • I created a console project called "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 file:

 <?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 solution project, add the post build event command, for example: team after assembly

when the test succeeds, I get the following output: valid config

when the check fails, I get this output: invalid config

0
source share

All Articles