我有一个 XML 文件列表,我必须从中获取特定行之后的字符串。
在文件中,我需要查找标签
Event
并获取属性值DLLRoutine
。
例如标签如下所示...
<Event Definition="Validate" DLLPath="" DLLName="Helper.dll" DLLClass="HelpMain"
DLLRoutine="pgFeatureInfoOnValidate_WriteToRegSelectedFeatures"
InputParameters="pTreeViewFeatureTreeServerOS" RunOnce="no"/>
我只需要获取
Dllroutine
值。如何使用 PowerShell 执行此操作?
假设您的 XML 结构类似于:
$xml = [xml]'
<Events>
<Event Definition="Validate" DLLPath="" DLLName="Helper.dll" DLLClass="HelpMain" DLLRoutine="pgFeatureInfoOnValidate_WriteToRegSelectedFeatures" InputParameters="pTreeViewFeatureTreeServerOS" RunOnce="no"/>
<Event Definition="Validate1" DLLPath="" DLLName="Helper.dll1" DLLClass="HelpMain1" DLLRoutine="pgFeatureInfoOnValidate_WriteToRegSelectedFeatures" InputParameters="pTreeViewFeatureTreeServerOS" RunOnce="no"/>
</Events>
'
#Or get it from a XML file
$xml = [xml](Get-Content $XMLPath)
$xml.Events.Event | Select DLLName
假设您的
Event
元素有一个 Events
元素根:
$xml.Events.Event.DLLName
我只在 Powershell 3 中测试过这个
您也可以使用 xpath 代替点符号:
$xml.SelectNodes('//Events/Event') | select DLLName
你可以使用
Select-XML
:
$xml = [xml]'
<Events>
<Event Definition="Validate" DLLPath="" DLLName="Helper.dll" DLLClass="HelpMain" DLLRoutine="pgFeatureInfoOnValidate_WriteToRegSelectedFeatures" InputParameters="pTreeViewFeatureTreeServerOS" RunOnce="no"/>
<Event Definition="Validate1" DLLPath="" DLLName="Helper.dll1" DLLClass="HelpMain1" DLLRoutine="pgFeatureInfoOnValidate_WriteToRegSelectedFeatures" InputParameters="pTreeViewFeatureTreeServerOS" RunOnce="no"/>
</Events>
'
($Select-XML -xml $xml -xpath "//Event/@DLLName").Node
尝试
$xml.Events.Event.DLLName
它将在版本 2 中工作,我尝试在几个脚本中使用 xpath,但每次都失败,所以直到我尝试点符号之前,我认为我做错了。