freakbn
freakbn

Reputation: 27

How to convert property in XML with powershell?

I try to convert xml from this:

<test>
    <sub ID="126754">
        <name>test</name>
    </sub>
    <sub ID="126769">
        <name>test2</name>
    </sub>
</test>

to this :

<test>
    <sub>
        <ID>126754</ID>
        <name>test</name>
    </sub>
    <sub>
        <ID>126769</ID>
        <name>test2</name>
    </sub>
</test>

I can read and loop in my file but I don't find how to convert ID=nnnnnn to <ID>nnnnnn</ID>

Upvotes: 0

Views: 81

Answers (1)

Kemal K.
Kemal K.

Reputation: 311

Try This

$newContent = @()
$test=gc C:\temp\xmll.xml

ForEach($Regel In $Text) {
  if($Regel -match "ID=\d{6}") {
    $newContent += "    <sub>"
    $newContent += "        <ID>$($Regel.Substring(8, 10))</ID>"

  } else {
    $newContent += $Regel
  }
}

$newContent

Upvotes: 1

Related Questions