Saturday, December 21, 2019

Resolving reading mal-formatted XML with PowerShell

This post explains a small issue faced while reading the XML configuration file values using a PowerShell script. You may have experienced the same issue while reading XML files with PowerShell. So, this post would help you to learn how we can resolve the issue.

Our requirement was to get the content of the App.config file and App.dll.config file to a PowerShell variable. It was possible to get the content of the App.config file to a PowerShell variable with following command.$appConfig is the App.config file path.
$doc = (Get-Content $appConfigFilePath) -as [Xml]

But when using the same command to read the content of the App.dll.config file it didn't return any value.
Once the App.dll.config is opened using visual studio code, it was possible to see there is a mal- formatted character at the end of App.dll.config file.



As a solution adding alternation to the command to remove mal-formatted character and assign the value to PowerShell variable as follows.

$doc = ((Get-Content -path $appConfigFilePath) -replace "`0", "") -as [xml];

This post explained how to resolve issues in reading mal-formatted xml file content with  PowerShell commands.

No comments:

Post a Comment