I have a PowerShell script that I use to remove certain tags from a XML file:
$Output = "C:\Users\Desktop\Resulttask.xml"
# Load the existing document
$Doc = [xml](Get-Content -Path C:\Users\Desktop\Test.xml)
# Specify tag names to delete and then find them
$DeleteNames = "Total" | ForEach-Object {
$Doc.ChildNodes.SelectNodes($_)
} | ForEach-Object {
$Doc.ChildNodes.RemoveChild($_)
}
$Doc.Save($Output)
My problem is that it work with a standard XML file like for example:
<html>
<body>b</body>
<Price>300</Price>
<Total>5000</Total>
</html>
But the problem is that the XML file is has to remove the tags from contains multiple prefixes like.
<ns:html xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns="2" release="1">
<ns:body>b</ns:body>
<ns:Price>300</ns:Price>
<ns:Total>5000</ns:Total>
</ns:html>
Then it does not remove the tag but is get a error as following
Exception calling "SelectNodes" with "1" argument(s): "Namespace Mangager or XSltContext needed. This query has a prefix, variable, or user-defined function." At line:2 char:5 + $Doc.ChildNodes.SelectNodes($_) + ------------------------------- + CategoryInfo : NotSpecified: (:) [], MethodInvocationExeption + FullyQuallifiedErrorId : DotNetMethodException
My question is how do I ensure that the PowerShell command ignores the prefix ns:
.