简体   繁体   English

如何使用Powershell在XML中转换属性?

[英]How to convert property in XML with powershell?

I try to convert xml from this: 我尝试从中转换xml:

<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> 我可以读取并循环进入文件,但是找不到如何将ID=nnnnnn转换为<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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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