0

I am trying to use PowerShell to format a messy XML file to a file with indentation where every tag starts on a new line.

The input file looks like:

<?xml version="1.0" encoding="WINDOWS-1252"?>
<RESULT>
<HEADER><ACCOUNT>035574623593</ACCOUNT><DATE_FROM>20150101</DATE_FROM><DATE_TO>20150526</DATE_TO><DATE_EQUAL></DATE_EQUAL><HD_DATIN>2015-11-27 15:16:52</HD_DATIN><HD_START>2015-11-27 15:16:52</HD_START><HD_ENGINE>WCV00228</HD_ENGINE><HD_QUERYID>984</HD_QUERYID></HEADER>
<FOOTER><FT_STATUS>NF</FT_STATUS><FT_RECORDS>2</FT_RECORDS><FT_SIZE>282</FT_SIZE><FT_ORDER>1</FT_ORDER><FT_STOP>2015-11-27 15:16:53</FT_STOP></FOOTER>
</RESULT>

This is a piece of my code where the formatting takes place:

function Format-XML {
  Param ([string]$xmlfile)

  $Doc=New-Object System.Xml.XmlDataDocument
  $doc.Load($xmlfile)
  $sw = New-Object System.IO.StringWriter
  $settings = New-Object System.Xml.XmlWriterSettings
  $settings.NewLineOnAttributes = $false
  $settings.Indent = 1
  $settings.NewLineOnAttributes = $true
  $writer = [System.xml.XmlWriter]::Create($sw, $settings)
  $doc.WriteContentTo($writer)
  $sw.ToString()
}

Format-XML ("${app}se-xf-out\UH99DEFI.BEMVS000.U$datum.T$counter") > "${app}se-xf-out\UH99DEFI.BEMVS001.U$datum.T$counter"

Can someone help me? The output file is empty (but still 1kb).

1 Answer 1

1

From the Microsoft blog, this seems to work just fine:

$Stuff = @"
<?xml version="1.0" encoding="WINDOWS-1252"?>
<RESULT>
<HEADER><ACCOUNT>035574623593</ACCOUNT><DATE_FROM>20150101</DATE_FROM><DATE_TO>20150526</DATE_TO><DATE_EQUAL></DATE_EQUAL><HD_DATIN>2015-11-27 15:16:52</HD_DATIN><HD_START>2015-11-27 15:16:52</HD_START><HD_ENGINE>WCV00228</HD_ENGINE><HD_QUERYID>984</HD_QUERYID></HEADER>
<FOOTER><FT_STATUS>NF</FT_STATUS><FT_RECORDS>2</FT_RECORDS><FT_SIZE>282</FT_SIZE><FT_ORDER>1</FT_ORDER><FT_STOP>2015-11-27 15:16:53</FT_STOP></FOOTER>
</RESULT>
"@

Function Format-XML ([xml]$xml, $indent=2) { 
    $StringWriter = New-Object System.IO.StringWriter 
    $XmlWriter = New-Object System.XMl.XmlTextWriter $StringWriter 
    $xmlWriter.Formatting = "indented" 
    $xmlWriter.Indentation = $Indent 
    $xml.WriteContentTo($XmlWriter) 
    $XmlWriter.Flush() 
    $StringWriter.Flush() 
    Write-Output $StringWriter.ToString() 
}
Format-XML ([xml]$Stuff) -indent 3

Generated output:

<?xml version="1.0" encoding="WINDOWS-1252"?>
<RESULT>
   <HEADER>
      <ACCOUNT>035574623593</ACCOUNT>
      <DATE_FROM>20150101</DATE_FROM>
      <DATE_TO>20150526</DATE_TO>
      <DATE_EQUAL>
      </DATE_EQUAL>
      <HD_DATIN>2015-11-27 15:16:52</HD_DATIN>
      <HD_START>2015-11-27 15:16:52</HD_START>
      <HD_ENGINE>WCV00228</HD_ENGINE>
      <HD_QUERYID>984</HD_QUERYID>
   </HEADER>
   <FOOTER>
      <FT_STATUS>NF</FT_STATUS>
      <FT_RECORDS>2</FT_RECORDS>
      <FT_SIZE>282</FT_SIZE>
      <FT_ORDER>1</FT_ORDER>
      <FT_STOP>2015-11-27 15:16:53</FT_STOP>
   </FOOTER>
</RESULT>
Sign up to request clarification or add additional context in comments.

Comments

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.