I need to Compare new names with name in XML file if name doesn't match then I would like to remove that actor from XML file, if the name doesn't existed then I would like to add it in.
$file = [xml]@'
<?xml version="1.0"?>
<data>
<title>Name</title>
<date>2019 Jan 01</date>
<actor>
<name>Actor Name 1</name>
<type>Actor</type>
<thumb>image.jpg</thumb>
</actor>
<actor>
<name>Actor Name 2</name>
<type>Actor</type>
<thumb>image.jpg</thumb>
</actor>
<genre>genre1</genre>
<genre>genre2</genre>
</data>
'@
I try below code but it doesn't work
$file = New-Object System.Xml.XmlDocument
$file.Load($file_path)
$file_data=$file.DocumentElement
$oldarray = $file_data.actor
$newarray=@(
"Actor 3",
"Actor Name 1"
)
Compare-Object -IncludeEqual -ReferenceObject $oldArray -DifferenceObject $newarray | Where-Object {
$w=$_.InputObject
if($_.SideIndicator -eq "=>"){
$a = $file_data.appendChild($file.CreateElement("actor"))
$a.AppendChild($file.CreateElement("name")).InnerText=$w.Trim()
$a.AppendChild($file.CreateElement("type")).InnerText="Actor"
}
if($_.SideIndicator -eq "<="){
$file_data.SelectSingleNode("//actor[text()='$w']") | foreach {[void]$_.parentnode.removechild($_)}
}
}