Is there a cmd tool to check a web.config file for validity?

When you have a malformed web.config and try and edit part of it in the GUI it will popup a box saying so and telling you the line it broke on.

Is there a way to access this functionality thought the cmd/powershell?


Solution 1:

You can use code like this:

$site = "Default Web Site"

try
{
    Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -location "$site" -filter "system.webServer" -name . | Out-Null
    Write-Output "All is Well"
}
catch [System.Exception]
{
    Write-Output $_ 
}

The output may include:

Get-WebConfigurationProperty : Filename: \\?\C:\inetpub\wwwroot\web.config
Line number: 7
Error: Configuration file is not well-formed XML

or

Get-WebConfigurationProperty : Filename: \\?\C:\inetpub\wwwroot\web.config
Line number: 5
Error: The configuration section 'foo' cannot be read because it is missing a section declaration