Powershell XML ImportNode

PowerShell Xml Import node Method

ImportNode of XmlDocument can be used to append the xmlNode from another Xml file.  ImportNode method contains has two arguments, first node to import and second boolean where true indicates that node and all its children/descendants would be imported and false to import the node only. $fileCopyTo = “copy to file path”
$fileCopyFrom = “copy from file path”
$copyToXml = New-Object System.Xml.XmlDocument
$copyFromXml = New-Object System.Xml.XmlDocument
$copyToXml.Load($fileCopyTo)
$copyFromXml.Load($fileCopyFrom)
$checkNode = $copyToXml.DocumentElement.SelectSingleNode(“element name”)
if($checkNode -eq $Null)
{
$setting = $copyToXml.ImportNode($copyFromXml.get_DocumentElement(), $true)
$copyToXml.DocumentElement.AppendChild($setting)
$copyToXml.Save($fileCopyTo)
}

Leave a Reply

Your email address will not be published. Required fields are marked *