Answered by:
remove section of an XML file
-
Hello everyone,
I have a many XML files that i would like to remove section of the each file. The structure of the section that i would like to remove looks like;
<References> <Reference Alias="CitrixPresentationServer5001"> <ID>Citrix.PresentationServer</ID> <Version>5.0.0.1</Version> <PublicKeyToken>493222c231fd24b5</PublicKeyToken> </Reference> <Reference Alias="IBMHardwareMgmtPackCommon200501"> <ID>IBM.HardwareMgmtPack.Common</ID> <Version>2.0.0.501</Version> <PublicKeyToken>e9ab6480c3dc2a83</PublicKeyToken> </Reference> <Reference Alias="CitrixLibrary5000"> <ID>Citrix.Library</ID> <Version>5.0.0.0</Version> <PublicKeyToken>493222c231fd24b5</PublicKeyToken> </Reference> </References>
I would like to remove one whole section of the reference in each file.<Reference Alias="IBMHardwareMgmtPackCommon200501"> <ID>IBM.HardwareMgmtPack.Common</ID> <Version>2.0.0.501</Version> <PublicKeyToken>e9ab6480c3dc2a83</PublicKeyToken> </Reference>
Is there way to achieve this with PowerShell?
Thank you.
Orhan Taskin
Question
Answers
-
Hello Orthan,
Sure, PowerShell can handle this. First convert the content of each file to XmlDocument, then use an XPath pattern to retrieve the Target node through the SelectSingleNode Method, if the target node is found, retrieve its Parent node – in this case the References node – with another XPath pattern and the SelectSingleNode Method. Now pass the Target node to a call to the Parent node’s RemoveChild to remove it. Finally call the XmlDocument’s Save Method to update the file.
$xPath = '//Reference[@Alias="IBMHardwareMgmtPackCommon200501"]'
$dir = '<your xml directory>' # <- modify this
foreach ($f in Get-ChildItem $dir *.xml) { # <- modify the filter as required
$file = $f.FullName
[Xml]$xml = Get-Content $file
$node = $xml.SelectSingleNode($xPath)
if ($node) {
$referencesNode = $xml.SelectSingleNode('//References')
Write-Host Removing node -ForegroundColor Yellow
[Void]$referencesNode.RemoveChild($node)
$xml.Save($file)
}
}
Make sure to assign the directory’s path where the Xml files are located and change the filter in the call to Get-ChildItem if necessary.
Note that if the Target node is not found, nothing is changed.
Robert Robelo- Marked as answer by Alan ZhuModerator Friday, July 01, 2011 7:37 AM
All replies
-
Hello Orthan,
Sure, PowerShell can handle this. First convert the content of each file to XmlDocument, then use an XPath pattern to retrieve the Target node through the SelectSingleNode Method, if the target node is found, retrieve its Parent node – in this case the References node – with another XPath pattern and the SelectSingleNode Method. Now pass the Target node to a call to the Parent node’s RemoveChild to remove it. Finally call the XmlDocument’s Save Method to update the file.
$xPath = '//Reference[@Alias="IBMHardwareMgmtPackCommon200501"]'
$dir = '<your xml directory>' # <- modify this
foreach ($f in Get-ChildItem $dir *.xml) { # <- modify the filter as required
$file = $f.FullName
[Xml]$xml = Get-Content $file
$node = $xml.SelectSingleNode($xPath)
if ($node) {
$referencesNode = $xml.SelectSingleNode('//References')
Write-Host Removing node -ForegroundColor Yellow
[Void]$referencesNode.RemoveChild($node)
$xml.Save($file)
}
}
Make sure to assign the directory’s path where the Xml files are located and change the filter in the call to Get-ChildItem if necessary.
Note that if the Target node is not found, nothing is changed.
Robert Robelo- Marked as answer by Alan ZhuModerator Friday, July 01, 2011 7:37 AM
-

