DevOps | Scripts | Automation

Powershell

How to create an XML document using PowerShell – Part 2?

In part-1, we have seen how we can create the parent and child nodes for the XML document. If you haven’t read through part-1 yet, check the below link and then continue this article.

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

In the last part, we had created parent node “catalog” and child nodes “book”. Continuing further, we need to add the other elements.

Now to add the other child nodes, we will have to perform the same commands.

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

$xmlWriter.WriteStartElement("author") # Starting Author Node
$xmlWriter.WriteEndElement()           # Ending Author Node
$xmlWriter.WriteStartElement("title") # Starting title node
$xmlWriter.WriteEndElement()          # Ending Title node 
$xmlWriter.WriteStartElement("genre") # Starting Genre Node
$xmlWriter.WriteEndElement()          # Ending Genre Node.

$xmlWriter.WriteEndElement() #Ending Book node bk101.

Output:

Adding Attributes

To add the attributes to the node, we need to use the WriteAttributeString method of the XmlTextWriter class.

$xmlWriter.WriteStartElement("book") # Starting book node bk101.
$xmlWriter.WriteAttributeString("id","bk101")

Similarly to add the author node attributes,

$xmlWriter.WriteStartElement("author")  # Starting Author Node
$xmlWriter.WriteAttributeString("id","1234")
$xmlWriter.WriteAttributeString("Country","Russia")
$xmlWriter.WriteEndElement()           # Ending Author Node

Here is the twist, if you have only Node with the description, for example, <title>XML Developer’s Guide</title> then instead of starting or ending the node elements you can directly write element string (Description) and it will automatically start and end the node element.

$xmlWriter.WriteElementString("title","XML Developer's Guide")
$xmlWriter.WriteElementString("genre","Computer")

So the code for the bk101 book ID will be,

$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.WriteStartElement("book") # Starting book node bk101.
$xmlWriter.WriteAttributeString("id","bk101")

$xmlWriter.WriteStartElement("author")  # Starting Author Node
$xmlWriter.WriteAttributeString("id","1234")
$xmlWriter.WriteAttributeString("Country","Russia")
$xmlWriter.WriteEndElement()           # Ending Author Node

$xmlWriter.WriteElementString("title","XML Developer's Guide")
$xmlWriter.WriteElementString("genre","Computer")

$xmlWriter.WriteEndElement() #Ending Book node bk101.

You can add other node attributes or the description using the above method.

If you like this article then please subscribe to my blog.

3 thoughts on “How to create an XML document using PowerShell – Part 2?

Comments are closed.