[英]Powershell Xml Attribute cast to concrete datatype
我有以下简单的xml文件
<?xml version="1.0" encoding="utf-8"?>
<MyRoot
MyGuid="99999999-9999-9999-9999-999999999999"
>
</MyRoot>
和代码
[xml]$xml = Get-Content "C:\myfile.xml"
$unTypeVariable = $xml.MyRoot | Select MyGuid
Write-Host "unTypeVariable = '$unTypeVariable'"
[GUID]guidValue = [GUID]($unTypeVariable)
第三行输出:
unTypeVariable = '@{MyGuid=99999999-9999-9999-9999-999999999999}'
在最后一行,我不断收到强制转换异常(在下面)。 我理解它,但是我不知道如何解决它。
无法将类型“ Selected.System.Xml.XmlElement”的“ @ {MyGuid = 99999999-9999-9999-9999-9999-999999999999}”值转换为类型“ System.Guid”。
我试过了:
[GUID]guidValue = [GUID]($unTypeVariable.Text)
[GUID]guidValue = [GUID]($unTypeVariable.Value)
[GUID]guidValue = [GUID]($unTypeVariable.InnerText)
[GUID]guidValue = [GUID]($unTypeVariable.'#text')
这可能是愚蠢的。
我不是超级专家。
我终于有了
$unTypeVariable = $xml.SelectSingleNode('//MyRoot').Attributes["MyGuid"].Value
[GUID]guidValue = [GUID]($unTypeVariable)
一位同事刚给我发了一条消息。
这也将起作用:
$unTypeVariable = $xml.MyRoot.MyGuid
[GUID]guidValue = [GUID]($unTypeVariable)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.