我正在編寫一個PowerShell腳本來操縱一些Windows安裝程序XML(WiX)。我在.NET 3.5中使用新的XML API來實現這一點,因爲我發現它比DOM更易於使用。下面的腳本片段斷然拒絕工作:爲什麼XDocument.Descendants()在PowerShell ISE中返回IEnumerator?
[System.Reflection.Assembly]::LoadWithPartialName("System.Xml.Linq") | Out-Null
# Basic idea: Iterate through en.wxl's l10ns, get string for each l10n, search all wxs files for that value.
pushd "C:\temp\installer_l10n"
$wxlFileName = "${pwd}\en.wxl"
$wxl = [System.Xml.Linq.XDocument]::Load($wxlFileName)
$strings = $wxl.Descendants("String")
$strings
$strings | foreach {
$_
}
popd
腳本應輸出的每個字符串<標籤>在單獨的行。我要得到它做一些更有趣一旦這個錯誤一直在解決;-)
XML文檔是一個標準的WiX本地化文件:
<?xml version="1.0" encoding="utf-8" ?>
<WixLocalization Culture="en-US" xmlns="http://schemas.microsoft.com/wix/2006/localization">
<String Id="0">Advertising published resource</String>
<String Id="1">Allocating registry space</String>
...
</WixLocalization>
$字符串不是$空(I」我已經明確地測試過了),如果我編寫主機$ wxl,我可以看到文檔已經加載。將$字符串傳送到Get-Member中會返回一個錯誤,指出「沒有對象被指定爲get-member」,write-host $字符串什麼也不做。我也嘗試了$ wxl.Descendants(「WixLocalization」),結果相同。像$ wxl.Root和$ wxl.Nodes這樣的東西按預期工作。使用PowerShell ISE調試,我發現$字符串已被設置爲IEnumerator,而不是預期的IEnumerable <XElement>。用一個MoveNext測試IEnumerator,然後用Current表示「Current =」,大概是$ null。
奇怪的是,相同的技術在以前的腳本中起作用。完全相同的代碼,但具有不同的變量名稱和字符串文字。並且剛剛嘗試調試該腳本(以驗證行爲),似乎它現在也顯示相同的行爲。
感謝您的回覆。由於這是工作中的問題,我將在週一回到辦公室時嘗試一下。 – alastairs 2009-02-07 09:32:07
雖然我接受了其他答案,但它確實有效,因爲它使用了我已有的LINQ代碼。 – alastairs 2009-02-09 10:19:09