gpt4 book ai didi

xml - Powershell无法读取xml文件?

转载 作者:行者123 更新时间:2023-12-02 23:53:44 36 4
gpt4 key购买 nike

我有一个Powershell脚本来读取xml文件。这是代码

$configPath = Join-Path $PSScriptRoot 'config.xml'
Write-Host $configPath
[xml]$XmlDocument = [xml](get-content $configPath)
Write-Host $XmlDocument
foreach($line in Get-Content $configPath) {
Write-Host $line
}

当我打印 $XmlDocument时,它显示为空字符串,但是该文件有效,因为当我执行for循环以一次读取第一行时,它可以工作并打印
<employees>
<employee id="101">
<name>Frankie Johnny</name>
<age>36</age>
</employee>
<employee id="102">
<name>Elvis Presley</name>
<age>79</age>
</employee>
<employee id="301">
<name>Ella Fitzgerald</name>
<age>102</age>
</employee>
</employees>

有人知道是什么问题吗?

谢谢

最佳答案

使用Write-Output,而不是Write-Host。我使用自己的XML文档进行了测试,[xml]$XmlDocument包含数据,但Write-Host不输出任何内容。我不能完全确定Write-Host为什么不打印$ XmlDocument的内容,但是我可以确定它与Write-Host周围的管道奇数有关(这会中断管道)。

您几乎应该始终避免使用Write-Host来代替Write-Output。另请参见Write-WarningWrite-VerboseWrite-ErrorWrite-Debug cmdlet将信息输出到STDOUT或控制台主机以外的其他流。请参阅this post for more information,以了解为什么应尽可能避免使用Write-Host,以及如何认为它有害。

关于xml - Powershell无法读取xml文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51249253/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com