0

Having an issue loading an Xml file from a powershell script. I want to read in a .config file and replace some of the appSettings but the file is not loading properly.

$path = "C:\Temp\psTest\App.config"

$XmlDocument = New-Object XML
$XmlDocument.Load($path)

Write-Output $XmlDocument

The output I get from that is:

PS C:\Temp\psTest> C:\Temp\psTest\config-work.ps1

xml                            configuration
---                            -------------
version="1.0" encoding="utf-8" configuration

If I debug into this script there are no child items in it. I did find a post that showed the first line to need removing but it wouldn't work for me. It does look as though it fails after the first line.

Is the first line the problem? If so how do I get rid of it so that I can load the file and manipulate it. That includes saving it back so is removing the first line going to be an issue there?

1 Answer 1

2

Your script loads the XML data just fine (otherwise you'd be seeing an error). Loading the XML data means that the XML text is getting parsed into an object structure. What you see in the output is just a representation of the top-level nodes (declaration and document root) in that structure.

For displaying the XML object in text form you could do something like this:

$xmlDocument.Save([Console]::Out)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I did that and it is indeed all there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.