Search code examples
xmlpowershellxml-parsingpowershell-4.0

Powershell Select Copy Paste XML NODE


I have an XMl which I'd like to expand.

  1. I want to select the 2. "Step" Node- > copy -> Change -Number- | -Name- and some inner-Text strings
  2. I want to select the 2. "Transition" Node -> copy -> Change -Number- | -Name- and some inner-Text strings
  3. I want to select the 2. "Connection" Node -> copy -> Change some inner-Text strings
  4. I want to select the penultimate "Connection" copy -> Change some inner-Text strings

Here are my XML (without the end-tags):

XML

Here are the Code that work fine in an other job:

$OPN_in ='C:\OPN\OPNOUT.xml'
$OPN_Out='C:\OPN\imp\OPNIMP.xml'


# 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
}


# erstelle eine Kopie des Knotens im Speicher
$NEU_NW = $Bsp_NW.Clone()


$NEU_NW.innerxml = $NEU_NW.innerxml -replace ("'Start'" -replace "'"),("'NEXT'" -replace "'")

# 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)

#Basisdatenändern
$xml.Document.'SW.Blocks.FC'.AttributeList.Name ='OPN_NEW'
$xml.Document.'SW.Blocks.FC'.AttributeList.Number = [String]($xml.Document.'SW.Blocks.FC'.AttributeList.Number+'00')


# speichere das geänderte xml unter neuem Namen
$xml.Save($OPN_Out)
#>

What not work is:

$Bsp_NW = $xml.SelectSingleNode("//Steps")

thanks for support.


Solution

  • Now It's working with:

    # Abfrage des ersten 'SW.Blocks.CompileUnit' Knotens
    $Bsp_NW = $xml.SelectSingleNode("//SW.Blocks.CompileUnit")
    
    $Bsp_NW = $xml.SelectSingleNode("//Steps")
    
    $NEW_Step = $Bsp_NW.step[1].Clone()
    $NEW_Step.Number
    

    I'm confusing...

    But Maybe you can give me a tipp How to select a step by using die "number" attribute ?