我正在写一个PowerShell脚本来处理某些Windows Installer XML(WiX的)。我使用.NET 3.5中新的XML API来做到这一点,因为我觉得它更容易使用的API比DOM工作。下面的脚本片段断然拒绝工作:

[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文档是一个标准的维克斯本地化文件:

<?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>

$字符串不是$空(我已经明确地测试了它),如果我写主机$ WXL,我可以看到,该文件已加载。管道$字符串到Get-Member将返回一个错误,指出“没有对象已指定给Get-Member”,并写主机$弦什么都不做。我也试过$ wxl.Descendants(“WixLocalization”)具有相同的结果。像$ wxl.Root和$ wxl.Nodes事情按预期工作。调试使用PowerShell ISE,我看到$串已被设置为IEnumerator的,而不是预期的IEnumerable <的XElement>。测试的IEnumerator与单一的MoveNext然后电流显示“当前=”,大概$空。

奇怪的是,同样的技术在以前的脚本工作。完全相同的代码,但具有不同的变量名和字符串。而刚刚尝试调试脚本太(验证的行为),似乎它现在也显示相同的行为。

有帮助吗?

解决方案

这个问题吸引了我,所以我做了一些摸索。很多在PowerShell中乱搞,并在网上搜索后,我发现您的解决方案。

幸得杰米汤姆森的实际代码。 http://dougfinke.com/blog/的index.php / 2007/08/07 /使用-xmllinq合的powershell /

缺少的部分是处理在XML文件中的命名空间。下面是应为你工作的代码:

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

$wxlFileName = "${pwd}\en.wxl"
$wxl = [System.Xml.Linq.XDocument]::Load($wxlFileName)
$ns = [System.Xml.Linq.XNamespace]”http://schemas.microsoft.com/wix/2006/localization”
$strings = $wxl.Descendants($ns + "String")
foreach ($string in $strings) {
    $string
}

其他提示

我知道你说你不者优先与DOM的工作,但没有任何理由,这并不为你工作?

[xml]$test = gc .\test.wml                                                                                        
$test                                                                                                             
$test.WixLocalization                                                                                             
$test.WixLocalization.String

这个输出:

PS> $ test.WixLocalization.String

标识#text结果 - -----结果 0广告发布的资源点击 1分配注册表空间

Foreach'ing以上不应该在该点太困难了。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top