Manipulating an XML file with empty elements

Hi all.

I am creating a Powershell script to modify an XML configuration file that I do not have any control over. It has empty elements entered as follows:-

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <TEST Id="attr" Source="anotherattr"></TEST>

If I open and save the above using Powershell as follows:-

$xml = [xml](Get-Content "xmltest.xml")

Then the empty element ends up split over multiple lines:-

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  <TEST Id="attr" Source="anotherattr">

This then unfortunately causes the an issue for the software which consumes this modified XML file again which I have no control over.

So the question is, if you are still reading this far, is there any way I can stop Powershell from doing that?


  • Why are you doing the save at all? PowerShell treats XML as a first class citizen. Much of PowerShell specifically relies on XML, and provide cmdlet and or one can use the .Net namespace to deal with XML. The only reason do a save is if you are specifically changing the XML file for some reason. One can dynamically parse XML using the built-in PowerShell / .Net features.

Reply Children
No Data