[英]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.