Maurice Klimek
Maurice Klimek

Reputation: 1092

Closing an xml file after reading it with Powershell

I'm making a script for publishing web applications with PowerShell.

Since I'm early in development I edit the script and the xml file alternately. Right now, the script is quite small and simple:

$settings = [xml](Get-Content c:\PublishData.xml)
$whereToProjectsAreLocally = $settings.publishing.globals.localprojectdir

But I do have a problem. Once I launch the script and check that everything works as planned I see that editing the xml file (via Notepad++) is impossible. I receive an error message that the save failed and I should check if the file is used by another application.

No other applications apart from Notepad++ and the Powershell ISE aren't using the file.

Closing the ISE does not solve the problem (closing the ISE everytime I launch the script isn't my dream solution either).

Closing the Notepad++ doesn't solve the problem either (although the newest version remembers changes even if I don't save them, but that's not the solution either).

I suppose there is something like an XmlReader in the background that I haven't closed. My question is: how do I close it after reading the content from the xml file?

EDIT: Same script works fine if the xml is not stored on C:\ but in a lower directory. How can I make it work in the root directory C:\ ?

Upvotes: 0

Views: 2029

Answers (1)

Brian
Brian

Reputation: 5049

Try this:

$xmlFile = 'c:\PublishData.xml'
$rawXML = Get-Content $xmlFile -Raw
$settings = [XML]$rawXML

Edit: Was just thinking, this may be another option:

$settings = [xml]([System.IO.File]::ReadAllLines($xmlFile))

Upvotes: 1

Related Questions