Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

PowerShell Create XML document with XmlWriter .net object

XML stands for eXtensible Markup Language it was designed to store and transport data and to be both human- and machine-readable. I wanted to create a simple readable XML files using PowerShell for few of the projects. There are native cmdlets (Export-CliXML and ConvertTo-XML) available in the Powershell to create XML file, but the data format in the xml file was generating the format I was looking for. I wanted plain XML file. To make the required format I used .net object [System.XML.XmlWriter].

Microsoft Powershell XML File .net object XML object xmlwriter new-object writestartelement writeelementstring.png

While writing the script, I have added few extra information to this XML for testing purpose. Those lines are safe to remove, if you don't want.

Download script here Generate-XMLWithNetObject.ps1 here or  this script is also available on github.com.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#Referenace Document
#https://docs.microsoft.com/en-us/dotnet/api/system.xml.xmlwriter.writeprocessinginstruction?view=net-5.0
#https://www.w3schools.com/xml/dom_intro.asp
$filePath = 'C:\Temp\logs\info.log'
#Step by step Article to create a XML Document using 

#Use XmlWriterSettings .net Object and Configure The XML format settings
$xmlsettings = New-Object System.Xml.XmlWriterSettings
$xmlsettings.Indent = $true
$indentChars = $(' ' * 4)
$xmlsettings.IndentChars = $indentChars 

#Create a empty XML file under given path with XML Settings
$xmlWriterObj = [System.XML.XmlWriter]::Create($filePath, $xmlsettings)

# Write the XML Declaration and set the XSL
$xmlWriterObj.WriteStartDocument()

#Define Additional XML format (Write the Processing Instruction node.)
$xmlWriterObj.WriteProcessingInstruction('xml-stylesheet', "type='text/xsl' href='style.xsl'") #can be removed this line

#Define the DocumentType node
$xmlWriterObj.WriteDocType('rootinfo', $null , $null, '<!ENTITY rootinfo>')  #can be removed this line

#Define a Comment node
$xmlWriterObj.WriteComment("sample XML") #can be removed this line

#Define Start the Root Element
$xmlWriterObj.WriteStartElement("Root") # <-- Start <Root> Element
    
    $xmlWriterObj.WriteStartElement('Object') # <-- Start <ChildObject>  Element

            $xmlWriterObj.WriteElementString('PropertyKey01','Value01')   # <-- Add Property key Value pair 1 Element
            $xmlWriterObj.WriteElementString('PropertyKey02','Value02')   # <-- Add Property key Value pair 2 Element
            $xmlWriterObj.WriteElementString('PropertyKey03','Value03')   # <-- Add Property key Value pair 2 Element

                $xmlWriterObj.WriteStartElement("SubChildObject") # <-- Start <SubChildObject> 
                
                    $xmlWriterObj.WriteEntityRef("h") #<-- add style and data #can be removed this line
                    $xmlWriterObj.WriteElementString("SubPropertyKey01","Value01")
                    $xmlWriterObj.WriteElementString("SubPropertyKey02","Value02")

                $xmlWriterObj.WriteEndElement() # <-- Add End <SubObject>

    $xmlWriterObj.WriteEndElement() # <-- Add End <Object>

    #Define CDATA
    $xmlWriterObj.WriteCData("This is some CDATA") #can be removed this line
    
$xmlWriterObj.WriteEndElement() # <-- Add End <Root> 

#This is End, Finalize and close the XML Document
$xmlWriterObj.WriteEndDocument()
$xmlWriterObj.Flush()
$xmlWriterObj.Close()

Useful Articles
PowerShell XML OperationStopped: No coercion operator is defined between types 'System.Object&' and 'System.Object'
Powershell Create new file if not exist, if exist Rename file
PowerShell Encode or Decode an WebURL
PowerShell convert string to base64 value
Powershell WPF Themes guide step by step
Part 2: Powershell WPF Themes guide step by step
Part 3: Powershell wpf MahApps.Metro theme step by step
Powershell WPF MahApps.Metro update theme library

Go Back

Comment

Blog Search

Page Views

12056117

Follow me on Blogarama