I have xml files where I want to paste the contents of one xml file into another. I decided to use LastChild and the InsertAfter method for this. So far this does not work for me.
Here is the parent.xml file:
<manifest> <manifestExecution> <assetDetail> <fileAsset fileAssetGuid="parentguid1"> <parentfile1 /> </fileAsset> <fileAsset fileAssetGuid="parentguid2"> <parentfile2 /> </fileAsset> </assetDetail> </manifestExecution> </manifest>
And here is the child.xml file:
<manifest> <manifestExecution> <assetDetail> <fileAsset fileAssetGuid="childguid1"> <childfile1 /> </fileAsset> </assetDetail> </manifestExecution> </manifest>
What I want to do is select the Asset node file from child.xml and paste it into parent.xml after the last fileAsset node file in parent.xml.
Here is my test code:
$parent = [xml] (Get-Content d:\temp\parent.xml) $parentnode = $parent.manifest.manifestExecution.assetDetail $child = [xml] (Get-Content d:\temp\child.xml) $childnode = $child.manifest.manifestExecution.assetDetail.InnerXml $parentnode.InsertAfter($childnode, $parentnode.LastChild)
Here is the msg message:
Cannot convert argument "0", with value: "<fileAsset fileAssetGuid="childguid1"> <childfile1 /></fileAsset>", for "InsertAfter" to type "System.Xml.XmlNode": "Cannot conver t the "<fileAsset fileAssetGuid="childguid1"><childfile1 /></fileAsset>" value of type "System.String" to type "System.Xml.XmlNode"." At line:5 char:24 + $parentnode.InsertAfter <<<< ($childnode, $parentnode.LastChild) + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
What am I doing wrong?
Keith source share