Press "Enter" to skip to content

Creating XML Documents from Powershell

We all know I have OCD when it comes to formatting. Using the built in PowerShell cmdlets (Export-XmlCli) to export XML left me with a mess of an XML Schema that I couldn’t apply a XSL Transform to. I needed something that would allow me to control the schema, and insert the XSL declaration. After doing quite a bit of online searching and research, I decided to try using the .NET class System.XML.XmlTextWriter. The code sample below is the basic snippet using the class and creating a document.

A couple of notes:

  • $filepath will need to be set to the full file path, using “.\” will not work.
  • If you are creating a complex file, you will want to note which tag you are closing with the WriteEndElement.

One Comment

  1. David Granlund David Granlund May 31, 2017

    Nice, but $xmlWriter.WriteEndElement doesn’t end an element, $xmlWriter.WriteEndElement() does. I ended up with a very interesting XML before I figured that out… 🙂

Leave a Reply