Creating XML files in PowerShell can be a powerful way to manage and automate data handling. This guide will show you how to create and format an XML document using XMLWriter in PowerShell.

Introduction

XML (Extensible Markup Language) is widely used for storing and transporting data. PowerShell, with its robust scripting capabilities, allows for the creation and manipulation of XML files. This guide is inspired by a detailed article on creating XML documents using XMLWriter, which you can find here.

PowerShell Function to Create XML

The following PowerShell function, CreateXMLGUI, demonstrates how to create an XML file and write data to it:

function CreateXMLGUI {
    $xmlpath = $PSScriptRoot + "\buildwindows10.xml"
    Write-Host "-------------------------------------------"
    Write-Host $xmlpath
    Write-Host "-------------------------------------------"

    if (Test-Path $xmlpath) { Remove-Item $xmlpath -Force -Recurse }
    
    # Set The Formatting
    $xmlsettings = New-Object System.Xml.XmlWriterSettings
    $xmlsettings.Indent = $true
    $xmlsettings.IndentChars = "    "

    # Set the File Name and Create The Document
    $XmlWriter = [System.XML.XmlWriter]::Create($xmlpath, $xmlsettings)

    # Write the XML Declaration and set the XSL
    $XmlWriter.WriteStartDocument()
    $XmlWriter.WriteProcessingInstruction("xml-stylesheet", "type='text/xsl' href='style.xsl'")

    $ValueISOWindows10  = $ISOWindows10.Text
    $ValueExportFolder  = $ExportFolder.Text
    $valueDriversFolder = $DriversFolder.Text
    $ValueMSUUpdate     = $MSUUpdate.Text

    # Start the Root Element
    $XmlWriter.WriteStartElement("Buildw10")
        $XmlWriter.WriteStartElement("Text")
            $XmlWriter.WriteElementString("ISOWindows10", "$ValueISOWindows10")
            $XmlWriter.WriteElementString("ExportFolder", "$ValueExportFolder")
            $XmlWriter.WriteElementString("DriversFolder", "$valueDriversFolder")
            $XmlWriter.WriteElementString("MSUUpdate", "$ValueMSUUpdate")
        $XmlWriter.WriteEndElement() # End 

        $XmlWriter.WriteStartElement("Appx")
        $AppxDelete = $Appx.SelectedItems
        foreach ($DelectAppx in $AppxDelete) {
            $XmlWriter.WriteElementString("Select", "$DelectAppx")
        }
        $XmlWriter.WriteEndElement() # End 

        $XmlWriter.WriteStartElement("Version")
        $Releases = $ListRelease.SelectedItems
        foreach ($Release in $Releases) {
            $XmlWriter.WriteElementString("Select", "$Release")
        }
        $XmlWriter.WriteEndElement() # End 

    $XmlWriter.WriteEndElement() # End 

    # Finalize and close the XML Document
    $XmlWriter.WriteEndDocument()
    $XmlWriter.Flush()
    $XmlWriter.Close()
}

Explanation of the Function

  • Setting the Path: The XML file path is set using the script root directory.
  • Removing Existing File: If the file already exists, it is deleted to ensure a fresh start.
  • Configuring XML Writer: The XMLWriterSettings object is created to define the formatting (indentation and indent characters).
  • Writing the XML Document: The XMLWriter object is used to start the document, add a stylesheet, and write elements and their values.
  • Adding Elements: Elements and sub-elements are added using WriteStartElement and WriteElementString methods.
  • Finalizing the Document: The document is finalized and closed using WriteEndDocument, Flush, and Close methods.

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.