简体   繁体   中英

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM