Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 为什么XDocument.subjects()在PowerShell ISE中返回IEnumerator?_Xml_Powershell_Linq To Xml_Powershell 2.0_Powershell Ise - Fatal编程技术网

Xml 为什么XDocument.subjects()在PowerShell ISE中返回IEnumerator?

Xml 为什么XDocument.subjects()在PowerShell ISE中返回IEnumerator?,xml,powershell,linq-to-xml,powershell-2.0,powershell-ise,Xml,Powershell,Linq To Xml,Powershell 2.0,Powershell Ise,我正在编写一个PowerShell脚本来操作一些Windows Installer XML(WiX)。我正在使用.NET3.5中新的XMLAPI来实现这一点,因为我发现它比DOM更容易使用。以下脚本片段断然拒绝工作: [System.Reflection.Assembly]::LoadWithPartialName("System.Xml.Linq") | Out-Null # Basic idea: Iterate through en.wxl's l10ns, get string for

我正在编写一个PowerShell脚本来操作一些Windows Installer XML(WiX)。我正在使用.NET3.5中新的XMLAPI来实现这一点,因为我发现它比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
脚本应该在单独的行上输出每个标记。一旦这个bug被解决,我将让它做一些更有趣的事情;-)

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>

广告发布资源
分配注册表空间
...
$strings不是$null(我已经显式地测试了它),如果我编写主机$wxl,我可以看到文档已经加载。将$strings管道传输到Get成员中会返回一个错误,指出“没有为Get成员指定对象”,而写入主机$strings不会执行任何操作。我还尝试了$wxl.subjects(“WixLocalization”),得到了同样的结果。像$wxl.Root和$wxl.Nodes这样的东西按预期工作。在调试PowerShell ISE时,我看到$strings已设置为IEnumerator,而不是预期的IEnumerable。使用单个MoveNext和电流测试IEnumerator表明“Current=”,大概是$null


奇怪的是,同样的技术在以前的脚本中也起了作用。代码完全相同,但变量名和字符串文字不同。刚刚试过调试该脚本(以验证其行为),现在它似乎也显示了相同的行为

我知道您说过您不喜欢使用DOM,但是有什么原因导致这不适合您吗

[xml]$test = gc .\test.wml                                                                                        
$test                                                                                                             
$test.WixLocalization                                                                                             
$test.WixLocalization.String
这将产生:

PS>$test.WixLocalization.String

Id#文本
------
0广告发布资源
1分配注册表空间


在这一点上进行讨论应该不会太困难。

我知道你说过你不喜欢使用DOM,但是有什么理由不适合你吗

[xml]$test = gc .\test.wml                                                                                        
$test                                                                                                             
$test.WixLocalization                                                                                             
$test.WixLocalization.String
这将产生:

PS>$test.WixLocalization.String

Id#文本
------
0广告发布资源
1分配注册表空间


这个问题引起了我的兴趣,所以我四处寻找。在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
}

这个问题引起了我的兴趣,所以我四处寻找。在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
}

谢谢你的回复。由于这是工作中的一个问题,我将在周一回到办公室后尝试。这确实有效,但我接受了另一个答案,因为它使用了我已有的LINQ代码。感谢您的回复。由于这是工作中的一个问题,我将在周一回到办公室后尝试。这确实有效,但我接受了另一个答案,因为它使用了我已经存在的LINQ代码。谢谢!我一定在谷歌搜索这篇文章的时候读了100遍,每次都错过了杰米·汤姆森的评论!在PowerShell中,LINQ对XML的使用似乎有些奇怪,因为这在C#中不会发生。谢谢!我一定在谷歌搜索这篇文章的时候读了100遍,每次都错过了杰米·汤姆森的评论!在PowerShell中,LINQ到XML似乎是一个变化莫测的问题,因为这在C#中是不会发生的。