Converting XML files to YAML or PSON

The other day, I needed to convert a whole stack of XML files to YAML.  Actually , I would have settled for a conversion to JSON, but for some reason, the built-in cmdlet wouldn’t do it. I was trying to figure out a way of doing the YAML conversion when I suddenly remembered I’d actually already published a way of doing it, in ‘Getting Data Into and Out of PowerShell Objects’. With relief, I got the routine out and tried it. It didn’t work, because I’d chosen to display an XML value as an innerXML string. This is the behaviour you might want, but not in this case.

There are two different ways of rendering XML values. You can render the object represented by the XML or you can do it just as an InnerXML string. The former method allows us to convert XML files directly into YAML or PSON.  Useful?  Sure. It is easier to demo than explain. First  rendering it as an object…

…giving …

and the other way, simply giving it as an XML fragment, (note the parameter if you want that behaviour)  is.

… giving …

It turned out that it was just as easy to handle the XML as an object as an XML document, and I’d already developed a way of doing a PowerShell object. After modifying the routine, and updating the article, I tried again. Nice. It worked a treat, and the YAML parser gulped it all in with no complaint. It wasn’t quick by any means with multi-megabyte XML files, but who cares when there are good videos to be watched? The PowerShell scripts are with the article here