0

#PSCXTip Formatting XML for better readability

XML is great for easily storing structured data and is pretty easy to work with in PowerShell. However, sometimes you run across XML that is so poorly formatted you lose out on XML’s human readability. Here’s a simple example – the entire set of XML on a single line:

<AFX_RIBBON attribute1="foo" attribute2="bar"><HEADER><VERSION>1</VERSION></HEADER><RIBBON_BAR><ELEMENT_NAME>RibbonBar</ELEMENT_NAME></RIBBON_BAR></AFX_RIBBON>

The space for the column is short so I’ve shown an example that isn’t too abusive. I’ve run across single line XML files that were thousands of characters wide. When you run into these situations, you’d really like a tool for pretty-print the XML for you. The PowerShell Community Extensions (1.2 or higher) comes with such a command – Format-Xml. Here’s an example of its usage based on the XML shown above:

PS> Format-Xml -InputObject $xml

<AFX_RIBBON attribute1="foo" attribute2="bar">
  <HEADER>
    <VERSION>1</VERSION>
  </HEADER>
  <RIBBON_BAR>
    <ELEMENT_NAME>RibbonBar</ELEMENT_NAME>
  </RIBBON_BAR>
</AFX_RIBBON>

Format-Xml has a few useful options like allowing you to specify that attributes should appear on new lines e.g.:

PS> Format-Xml -InputObject $xml –AttributesOnNewLine

<AFX_RIBBON
  attribute1="foo"
  attribute2="bar">
  <HEADER>
    <VERSION>1</VERSION>
  </HEADER>
  <RIBBON_BAR>
    <ELEMENT_NAME>RibbonBar</ELEMENT_NAME>
  </RIBBON_BAR>
</AFX_RIBBON>

There are also options for configuring the IndentString, setting the XML conformance level and omitting the XML declaration. Keep in mind you can load an XML, reformat it using Format-Xml and save it back out e.g.:

C:\PS> ${c:data.xml} | Format-Xml -AttributesOnNewLine > data.xml

The above trick requires that data.xml be in the current directory. Still it is a handy way to read & write the same file in a one-liner.

Note: There are many more useful PowerShell Community Extensions (PSCX) commands. If you are interested in this great community project led by PowerShell MVPs Keith Hill and Oisin Grehan, give PSCX a try at http://pscx.codeplex.com.

Filed in: Columns, Tips and Tricks Tags: , ,

Leave a Reply

Submit Comment

© 2016 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: