You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何PowerShell中需对Join-Path返回值显式转字符串赋值给XmlNode?

关于PowerShell中Join-Path赋值XML属性需显式转换的问题

根据官方文档,Join-Path函数返回字符串类型,理论上可直接用作字符串值,但实际操作中,若不对Join-Path的结果进行显式转换,PowerShell会抛出如下错误:

Cannot set "path" because only strings can be used as values to set XmlNode properties.
At C:\Users\Rayman\Downloads\example.ps1:6 char:1
+ $node.path = (Join-Path 'C:\Users\Rayman\Downloads' 'VSCode-win32-x64 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], SetValueException
    + FullyQualifiedErrorId : XmlNodeSetShouldBeAString

为何需要对一个本已是字符串类型的结果进行显式字符串转换?以下是最小复现示例:

脚本代码

$xmlPath = 'C:\Users\Rayman\Downloads\example.xml'
[xml]$xml = Get-Content $xmlPath
$node = $xml.SelectSingleNode('//Software')
$node.SetAttribute('path', '')
# 不清楚为何必须用[string]转换字符串类型变量
$node.path = [string](Join-Path 'C:\Users\Rayman\Downloads' 'VSCode-win32-x64-1.73.1')

XML文件

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Software id="VSCode" />
</Configuration>

使用的PowerShell版本为5.1.19041.5965。

内容的提问来源于stack exchange,提问作者Rayman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 20:52:06