DevOps | Scripts | Automation

Powershell

How to create an XML document using PowerShell?

To create an XML (eXtensible Markup Language) file there are a few methods and we will use the XMLTextWriter .Net method to create an XML file using PowerShell. Consider the below XML file as an example that we need to create using PowerShell.

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author id="1234" Country="Russia"></author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
   </book>
   <book id="bk102">
      <author id="2345" Country="US"></author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
    </book>
    <book id="bk103">
      <author id="5678" Country="India"></author>
      <title>Time Value</title>
      <genre>Non-Fiction</genre>
    </book>
</catalog>

The above examples ‘ some part is considered from the MS site below and the file is modified for the better understanding.

https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ms762271(v=vs.85)

In this article, we are using XMLTextWriter class. To learn more about its properties and methods, check the below link.

https://docs.microsoft.com/en-us/dotnet/api/system.xml.xmltextwriter?view=net-6.0

By taking the reference of the above class, we will create a new object for the XmlTextWriter class.

$xmlWriter = New-Object System.XMl.XmlTextWriter("C:\Temp\BooksRecord.xml", $Null)

The above command will create a new XML file named BooksRecord.xml in the directory C:\Temp.

Formatting the XML file.

$xmlWriter.Formatting = 'Indented'
$xmlWriter.Indentation = 1
$XmlWriter.IndentChar = "`t"

Here, we are making XML file Indented so it will be properly formatted when created, you can also choose space (” “) instead of the tab character “`t”.

To start XML document writing begin with,

$xmlWriter.WriteStartDocument()

To end the document,

$xmlWriter.WriteEndDocument()
$xmlWriter.Flush()
$xmlWriter.Close()

Now, we need to write the code in between of this section WriteStartDocument() and WriteEndDocument().

Before the start of creating a document, let’s understand what XML files elements are called. In the above Books.xml example,

  • Catalog is called the root node or simply element.
  • Similarly, book, author, title, genre are called the child nodes or elements.
  • id, Country, code are called the attributes.
  • Text between the nodes are called the description. <title>XML Developer’s Guide</title>, XML Developer’s Guide is called the description.

So, let’s first create the catalog node.

$xmlWriter.WriteStartElement("catalog")  # catalog Start Node
$xmlWriter.WriteEndElement()  # catalog end node.

The above code will look like,

$xmlWriter = New-Object System.XMl.XmlTextWriter("C:\Temp\BooksRecord.xml",$null)
$xmlWriter.Formatting = 'Indented'
$xmlWriter.Indentation = 1
$XmlWriter.IndentChar = "`t"

$xmlWriter.WriteStartDocument()

$xmlWriter.WriteStartElement("catalog")  # catalog Start Node
$xmlWriter.WriteEndElement()  # catalog end node.

$xmlWriter.WriteEndDocument()
$xmlWriter.Flush()
$xmlWriter.Close()

Output:

So far we have just created a catalog node. Whenever we start writing creating the element, we should know where to end it otherwise the child nodes will get created. For example, we now need to create the book node then we will create it before ending the catalog node because the book node is the child node of the catalog node.

$xmlWriter.WriteStartElement("catalog")  # catalog Start Node

$xmlWriter.WriteStartElement("book") # Starting book node.
$xmlWriter.WriteEndElement() #Ending Book node.

$xmlWriter.WriteEndElement()  # catalog end node.

Output:

Here, we need three book nodes then,

$xmlWriter.WriteStartElement("book") # Starting book node bk101.
$xmlWriter.WriteEndElement() #Ending Book node bk101.

$xmlWriter.WriteStartElement("book") # Starting book node bk103.
$xmlWriter.WriteEndElement() #Ending Book node bk102.

$xmlWriter.WriteStartElement("book") # Starting book node bk103.
$xmlWriter.WriteEndElement() #Ending Book node bk103.

Output:

If you have a large file to create then you can use the input from the csv of the text file and create a loop.

In the next part, we will see how we can add the Attributes and Description to these nodes.

https://theautomationcode.com/how-to-create-an-xml-document-using-powershell—part-2