Powershell Tip : Use XML Indentation to make it more readable


Hey guys!

Today’s blog post is a quick Powershell tip which is useful when we’ve to generate Dynamic XML using a Powershell script, if the file gets huge it gets difficult to read the XML data , hence in such a case Indentation comes handy.

HOW  TO MAKE IT WORK ?

Use System.IO.StringWriter  and System.XMl.XmlTextWriter classes  to build/write a XML string in an underlying String builder, containing XML data that complies with W3C (World wide web consortium) XML 1.0 standards.

You’ve just take care about two XmlTextWriter properties to indent your XML’s, those are

formatting

Make sure you make the property Formatting as indented  and provide an positive integer value to Indentation property for number of chars to write for each level in XML hierarchy.

formatting

gif

SCRIPT : 

Function Format-XMLIndent
{
[Cmdletbinding()]
[Alias("IndentXML")]
param
(
[xml]$Content,
[int]$Indent
)
# String Writer and XML Writer objects to write XML to string
$StringWriter = New-Object System.IO.StringWriter
$XmlWriter = New-Object System.XMl.XmlTextWriter $StringWriter
# Default = None, change Formatting to Indented
$xmlWriter.Formatting = "indented"
# Gets or sets how many IndentChars to write for each level in
# the hierarchy when Formatting is set to Formatting.Indented
$xmlWriter.Indentation = $Indent
$Content.WriteContentTo($XmlWriter)
$XmlWriter.Flush();$StringWriter.Flush()
$StringWriter.ToString()
}
view raw IndentXML.ps1 hosted with ❤ by GitHub

Hoping this would be useful for you, Thanks for reading ! 🙂

Prateek Singh

 

 

5 thoughts on “Powershell Tip : Use XML Indentation to make it more readable

  1. VMware Certified Advanced Professional 6 (Desktop and Mobility Deployment) – The industry-recognized VCAP6-DTM Deploy certification validates that you know how to deploy and optimize VMware Horizon 6 (with View) environments. It demonstrates that you have the understanding and skills essential to leverage best practices to provide a scalable and dependable Business Mobility platform for your organization. Some of the topics include: Configuring and managing Horizon View components, configuring cloud pod archituecture, configuring Group Policy settings related to Horizon View, Configuring and optimizing desktop images for Horizon View & Mirage, Configuring and managing App Volumes AppStacks, Configuring desktop pools, Configuring and deploying ThinApp packaged applications, Configuring VMWare Identity Manager, etc.Szumigalski.com was designed by Sebastian to propagate his interest for PowerShell & VMWare. Sebastian is an IT skilled professional working in Singapore for over 15 years who is continually seeking new technologies to refine his technical competencies & knowledge. Ever since, Sebastian has joined up with PowerShell User Group & VMWare VMug group, and has been involved in most of the functions held in Singapore. This website will highlight the way Sebastian are able to automate his everyday jobs applying PowerShell. You can get study guides readily available for the VCAP6-DTM exam, which were personally created by Sebastian. Sebastian is licensed with VCAP6-DTM, and is proficient with virtualization & server maintenance from 4 years experience in automation. The need for VMWare knowledgable admins and engineers are ever-increasing in the current technology sector. Study much more about PowerShell & VMWare at Szumigalski.com!

    Like

Leave a comment