This blog provides a simple example of using PowerShell to read and write XML nodes and attribute names.
Using PowerShell to Write XML
In this example, we first create a new [System.Xml.XmlDocument]
object to represent our XML document. We then create a root node and append it to the document. We create a child node, add an attribute and inner text, and append it to the root node.
Finally, we save the XML document to a file using the Save
method.
Using PowerShell to Read XML
In this example, we first load an XML file using the Get-Content
cmdlet and cast it as an [xml]
object using the PowerShell type accelerator. We can then access specific elements and attributes within the XML document using dot notation.
Using PowerShell to Iterate Through XML Nodes and Attributes
Let’s suppose our XML file has more than one child node like so:
We might then want to loop through these child nodes to read each node text and attribute. We can do this like so:

