Updating and Writing XML Files with PowerShell

Manipulating XML files with PowerShell is something that we’re having to accomplish more and more internally. Microsoft App-V 5.0 and RES Workspace Manager utilise XML files extensively. Whilst we can manually tweak them, there’s nothing like automating things for consistency and speed!

I have seen a lot of “rip and replace” and “find and replace” script examples, but ensuring that the correct elements/nodes are added in the correct place can be troublesome. The only real option is to utilise the built-in .Net Framework XML objects. Hopefully this post will lay the basis for some more App-V 5.0 focused blogs in the future Open-mouthed smile.

There are lots of forum posts out there that detail snippets of information, but a lot of it is trial and error. As a starting point you might want to read up here first. Reading XML files is one thing, but as the previous article mentions, inserting (multiple) elements can be quite convoluted. Note: I’m using the XmlDocument .Net object here but it is possible to utilise other .Net classes, e.g. XPath and/or XDocument.

Here is the example we’ll use in this post. Apologies; the XML formatting is stripped by the plugin:

Creating XML Elements

If we wish to add a new employee record to the above document, we need to create a new XML element (or node). Firstly we need to load the XML document:

Now we can create our new element/node, append it to the parent reference and save it:

To shorten this we can just use this (see here for more information on the CreateElement method):

If we examine the resulting XML file we’ll find the following (note the new empty element):

 Adding XML Attributes

To add the ID attribute tag in the XML document, we need to create a new XmlDocument Attribute and then attach it to our newly created element. We can create an XmlDocument attribute on our element with the following code:

Now our resulting XML file looks like this:

Adding Nested XML Elements

Knowing how to add elements we can simply create our sub elements and attach them to our newly created parent reference:

The resulting XML file now looks like:

Adding XML Text Nodes

Unbeknownst to me, when you have text within an element tag, i.e. <name>Iain Brighton</name>, this is known as a text node (at least in XDocument speak). This is probably the bit that took longest to work out.

To add the text node we can use the following code:

Et voilà!

 Full PowerShell Code Snippet

Here is the full code listing:

6 Comments

  1. Mark Author June 17, 2014 (5:36 pm)

    Very helpful, thanks

  2. azedine Author August 1, 2015 (7:13 pm)

    but if we want right ur script and excute the script that we give a lot of error :
    Impossible d’appeler une méthode dans une expression Null.
    Au caractère H:\shell.ps1:3 : 1
    + $newXmlEmployee = $xmlDoc.employees.AppendChild($xmlDoc.CreateElement(“employee” …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation : (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

  3. Iain Brighton Author August 14, 2015 (10:03 am)

    Hi Azedine,

    It looks like you do not have anything assigned to the $xmlDoc variable. You will need to have a XML file on disk (called “employees.xml”) to use the above examples.

    Thanks, Iain

  4. moradkh Author March 19, 2017 (6:14 pm)

    YOU ARE THE BEST !!!!

  5. Rinzler Author September 13, 2017 (12:35 pm)

    Hi,
    Thank you a lot for your example. But if you want to add an element in the employee with the id = 101. How you will do that ?
    Thanks for your reply

Trackbacks & Pingbacks

  1. We Are Updating Our Files September 17, 2016 (8:41 pm)

    […] Updating and Writing XML Files with PowerShell | Virtual … – Updating and Writing XML Files with PowerShell … Now we can create our new element … We have used Virtual Engine’s consultancy services on a number of … […]

Leave a Reply

Archives

Categories