I have a CSV-List to replace Strings in an XML.
Now the CSV looks like:
First;Secound One; Third
Name;Beschreibung Neu;Func1
Number;handout her, func2
The Code is:
#Splatennamen auslesen
$CSVSpalten = $CSVData | Get-member -MemberType 'NoteProperty' | Select-Object -ExpandProperty 'Name'
# xml Objekt erstellen
$xml = New-Object XML
# xml laden
$xml.Load($OPN_in)
# Abfrage des ersten 'SW.Blocks.CompileUnit' Knotens
$Bsp_NW = $xml.SelectSingleNode("//SW.Blocks.CompileUnit")
# abbrechen wenn kein Knoten gefunden wurde.
if (!$Bsp_NW){
throw "Error, 'SW.Blocks.CompileUnit' Node not found!"
exit 1
}
$z=0
foreach($Zeile in $CSV_reseve)
{
# erstelle eine Kopie des Knotens im Speicher
$NEU_NW = $Bsp_NW.Clone()
#Texte Ersetzen
foreach($spalte in $CSVSpalten)
{
#$NEU_NW.innerxml
write-Host "ALt: $spalte NEU: $Zeile.$spalte "
$NEU_NW.innerxml = $NEU_NW.innerxml -replace ("$spalte" -replace "'"),($Zeile."$spalte" -replace "'")
#ID's verändern
$z+=1
$NEU_NW.innerxml = $NEU_NW.innerxml.Replace(' ID="',' ID="'+($z*10).ToString())
#>
}
# id anpassen
$NEU_NW.Id = (100*$z).ToString()
# hänge den kopierten Knoten im selben Parent wie vom Original wieder ins XML ein
$Bsp_NW.ParentNode.InsertAfter($NEU_NW,$Bsp_NW)
}
Problem is the row:
$NEU_NW.innerxml = $NEU_NW.innerxml -replace ("$spalte" -replace "'"),($Zeile."$spalte" -replace "'")
I think its because the Space in the Name of the 2. Colum and the Uses in -> $Zeile."$spalte" <-.
How can is Correct it?
Try replacing this line, as yours is a odd way to deal with the csv import.
$CSVData= import-csv $CSVFile -encoding -utf8 -delimiter ';'
If it still does not work, then try using this to modify the $spalte before the replace.
[string]$Spalte2 = $Spalte -replace "'"
$NEU_NW.innerxml = [string]$NEU_NW.innerxml -replace $spalte2,$Zeile.$Spalte2
See if removing the -replace sorts things.