XML Powershell get and remove element node

I want to remove one element from MsDtsSrvr.ini, namely this whole node:

<Folder xsi:type="SqlServerFolder">

File looks like this:

<?xml version="1.0" encoding="utf-8"?>
<DtsServiceConfiguration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Folder xsi:type="SqlServerFolder">
    <Folder xsi:type="FileSystemFolder">
      <Name>File System</Name>

What I am failing to do is to get this whole node into variable, unless I do something like:

$sqlserverFolder = $xmlFile.SelectSingleNode("/DtsServiceConfiguration/TopLevelFolders/Folder")

This achieves what I want, however obvious problem with that is I am not selecting this particualar node, I suppose powershell is just selecting first one. Which I’d prefer to avoid to understand what’s going on really.

When I select all nodes using this script:


I get following results:

type             Name        ServerName
----             ----        ----------
SqlServerFolder  MSDB        .         
FileSystemFolder File System     

So I tried to run something like this:


but it does not return any results. How can I grab this particular node so I can remove it?

>Solution :

Use the local-name() XPath function to filter on the xsi:type attribute by it’s local name (type):

$xmlFile.SelectSingleNode("/DtsServiceConfiguration/TopLevelFolders/Folder[@*[local-name()='type' and . ='SqlServerFolder']]")

Leave a Reply