Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.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
如何在Powershell中迭代XML?_Xml_Powershell - Fatal编程技术网

如何在Powershell中迭代XML?

如何在Powershell中迭代XML?,xml,powershell,Xml,Powershell,我将此XML文档保存在文本文件中: <?xml version="1.0"?> <Objects> <Object Type="System.Management.Automation.PSCustomObject"> <Property Name="DisplayName" Type="System.String">SQL Server (MSSQLSERVER)</Property> <Property N

我将此XML文档保存在文本文件中:

<?xml version="1.0"?>
<Objects>
  <Object Type="System.Management.Automation.PSCustomObject">
    <Property Name="DisplayName" Type="System.String">SQL Server (MSSQLSERVER)</Property>
    <Property Name="ServiceState" Type="Microsoft.SqlServer.Management.Smo.Wmi.ServiceState">Running</Property>
  </Object>
  <Object Type="System.Management.Automation.PSCustomObject">
    <Property Name="DisplayName" Type="System.String">SQL Server Agent (MSSQLSERVER)</Property>
    <Property Name="ServiceState" Type="Microsoft.SqlServer.Management.Smo.Wmi.ServiceState">Stopped</Property>
  </Object>
</Objects>

在本例中,我希望使用SQL Server MSSQLSERVER输出一个字符串,PowerShell具有内置的XML和XPath函数。 您可以将Select Xml cmdlet与XPath查询一起使用,从Xml对象中选择节点,然后 .Node.“text”以访问节点值

[xml]$xml=获取内容$serviceStatePath $nodes=选择Xml//Object[Property/@Name='ServiceState'和Property='Running']/Property[@Name='DisplayName']$Xml $nodes | ForEach对象{$\.Node.'text'} 或更短

[xml]$xml=获取内容$serviceStatePath 选择Xml//Object[Property/@Name='ServiceState'和Property='Running']/Property[@Name='DisplayName']$Xml| %{$\.Node.'text'}
您也可以在不使用[xml]强制转换的情况下执行此操作。尽管xpath本身就是一个世界

或者正是这样,xpath是区分大小写的。两者的输出相同:

$xml = (select-xml -xpath /Objects/Object/Property -path stack.xml).node
$xml


Name         Type                                                #text
----         ----                                                -----
DisplayName  System.String                                       SQL Server (MSSQLSERVER)
ServiceState Microsoft.SqlServer.Management.Smo.Wmi.ServiceState Running
DisplayName  System.String                                       SQL Server Agent (MSSQLSERVER)
ServiceState Microsoft.SqlServer.Management.Smo.Wmi.ServiceState Stopped

我添加了一些解释。您还可以使用熟悉的属性语法浏览文档:$xml.Objects.Object |?{$\.ServiceState-eq Running}.displayname显式转换为[xml]为我完成了这项工作。如果不进行转换,它将xml作为字符串返回,解析将不起作用。当然没有错误。这是在哪个版本的Powershell中引入的?Get Content是加载XML的错误方法,因为它不尊重XML头的编码属性。大多数情况下,我们很幸运,只是因为许多XML文档都是UTF-8编码的,而UTF-8恰好是当前get内容使用的默认编码。但这仍然是错误的,有关更多详细信息,请参见XML似乎是直接从内存中的对象序列化的。有没有理由不将它们反序列化回内存?
$xml = (select-xml -xpath / -path stack.xml).node
$xml.objects.object.property
$xml = (select-xml -xpath /Objects/Object/Property -path stack.xml).node
$xml


Name         Type                                                #text
----         ----                                                -----
DisplayName  System.String                                       SQL Server (MSSQLSERVER)
ServiceState Microsoft.SqlServer.Management.Smo.Wmi.ServiceState Running
DisplayName  System.String                                       SQL Server Agent (MSSQLSERVER)
ServiceState Microsoft.SqlServer.Management.Smo.Wmi.ServiceState Stopped