Powershell会更改元素的值。如何禁用它?

时间:2012-11-27 01:24:56

标签: xml powershell

我正在使用流动的代码来处理一些xml。但它改变了价值! 我怎么能纠正?

$xml =[xml]@'
<?xml version="1.0" encoding="UTF-8"?><zabbix_export><hosts><host name="Template_Snmptrap"><dns></dns></host></hosts><dependencies /></zabbix_export>
'@
$random=Get-Random
$filename="{0}\{1}.xml" -f $env:temp,$random
$xml.save($filename)
get-content $filename

结果

PS C:\Users\33333> get-content $filename
<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
  <hosts>
    <host name="Template_Snmptrap">
      <dns>
      </dns>
    </host>
  </hosts>
  <dependencies />
</zabbix_export>
PS C:\Users\33333>

the value before and after

1 个答案:

答案 0 :(得分:1)

在$ xml中使用XML后,将PreserveWhitespace属性设置为$true,例如:

$xml =[xml]@'
<?xml version="1.0" encoding="UTF-8"?><zabbix_export><hosts><hostname="Template_Snmptrap"><dns></dns></host></hosts><dependencies /></zabbix_export>
'@
$xml.PreserveWhitespace = $true