I need to delete empty nodes from xml file.
ex:
<Keys>
<Key Name="Topic" />
<Key Name="Topic" />
<Key Name="Topic" />
<Key Name="Keyword" />
<Key Name="Keyword" />
<Key Name="Keyword" />
<Key Name="Keyword">Musk deer</Key>
<Key Name="Keyword">Hunting</Key>
<Key Name="Keyword">Trapping</Key>
<Key Name="Keyword">Mythology</Key>
<Key Name="Topic">Traditional narrative</Key>
</Keys>
The idea is remove this nodes:
<Key Name="Topic" />
<Key Name="Topic" />
<Key Name="Topic" />
<Key Name="Keyword" />
<Key Name="Keyword" />
<Key Name="Keyword" />
Any suggestion, to help me on this?
Best
You can use XML Object in PowerShell like this:
$FileContent = New-Object -Typename XML
# Load file
$FileContent.Load("C:\Temp\in.xml")
$Nodes = $FileContent.SelectNodes("//Key")
# For each node
foreach ($Node in $Nodes) {
if ($Node.InnerXml -ne "") {
$Node.ParentNode.RemoveChild($Node)
}
}
# Save file in UTF-8
$ConfigFileEncoding = New-Object System.Text.UTF8Encoding($false)
$ConfigFileWriter = New-Object System.XML.XMLTextWriter("C:\Temp\out.xml", $ConfigFileEncoding)
$ConfigFileWriter.Formatting = 'Indented'
$FileContent.Save($ConfigFileWriter)
$ConfigFileWriter.Close()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.