I would like to fix some xml files seperated on multiple sites and document libraries in powershell. I have in a csv file the url of all these xml documents. So I already have some loop for each xml path. But how can I modify the content of these xml files? Do I need to download the file, modify it local and upload it again?
1 Answer
Yes, we need to download the XML file and update as per your need then upload back to the same location:
For update sample, you can refer the below PowerShell:
$Path = "\\spsite\sites\subsite\Library\"
$Files = Get-ChildItem $Path | where {$_.extension -eq ".xml"}
foreach($file in $Files){
$xml = New-Object XML
$xml.PreserveWhitespace = $true
$xml.Load($file.fullname)
$xml.myFields.PhoneNumber = "234-5678"//update your fields on the form
$xml.Save($file.fullname)
}
Reference:
Powershell to replace text in infopath XML files
OR
If your XML files are relatively smaller in size - then you may use the MemoryStream - MemoryStream sample as below:
$Stream = New-Object System.IO.MemoryStream
$UDCX.Save($Stream)
$document.SaveBinary($Stream.ToArray())
Reference:
Using powershell to read/modify/rewrite sharepoint xml document