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"?>
<ROOT>
    <TEST Id="attr" Source="anotherattr"></TEST>
</ROOT>

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

$xml = [xml](Get-Content "xmltest.xml")
$xml.save("$pwd\xmltest.xml.new")

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

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

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?

TIA.

Parents
  • 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