Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.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中的键值对_Xml_Powershell - Fatal编程技术网

直接访问XML中的键值对

直接访问XML中的键值对,xml,powershell,Xml,Powershell,给定这个示例XML数据,是否可以直接访问密钥 例如:$xml.root.User\u Blob.LogonMethod <?xml version="1.0" encoding="utf-16"?> <root> <User_Blob> <Item> <Key>LogonMethod</Key> <Value>prompt</Value> </Item

给定这个示例XML数据,是否可以直接访问密钥

例如:$xml.root.User\u Blob.LogonMethod

<?xml version="1.0" encoding="utf-16"?>
<root>
  <User_Blob>
    <Item>
      <Key>LogonMethod</Key>
      <Value>prompt</Value>
    </Item>
    <Item>
      <Key>ServerURLEntered</Key>
      <Value>http://myserver/config.xml</Value>
    </Item>
    <Item>
      <Key>ServerURLListUsers</Key>
      <Value>
        <LSOption>http://myurl/config.xml</LSOption>
        <LSOption>http://myurl</LSOption>
      </Value>
    </Item>
    <Item>
      <Key>UserDisplayDimensions</Key>
      <Value>fullscreen</Value>
    </Item>
  </User_Blob>

logon方法
促使
ServerURLEntered
http://myserver/config.xml
ServerURLListUsers
http://myurl/config.xml
http://myurl
用户显示维度
全屏
试试这个:-

[xml]$xmlObject = (New-Object System.Net.WebClient).DownloadString("Filepath")   

Write-Host $xmlObject.root.User_Blob.Item.Key

$xmlObject = New-Object XML
$xmlObject.Load("YourFilePath")
$xmlObject.root.User_Blob.Item.Key
要获取LogonMethod的值,请尝试以下方法:-

($xmlObject.root.User_Blob.Item | Where-Object { $_.Key -eq 'LogonMethod' }).Value

还有一种方式:-

$xmlObject.selectSingleNode("/root/User_Blob/Item[Key = 'LogonMethod']/Value").get_innerXml()

就我个人而言,当需要对象时,我会使用
选择Xml

$c = [xml]'<?xml version="1.0" encoding="utf-16"?>
<root>
  <User_Blob>
    <Item>
      <Key>LogonMethod</Key>
      <Value>prompt</Value>
    </Item>
    <Item>
      <Key>ServerURLEntered</Key>
      <Value>http://myserver/config.xml</Value>
    </Item>
    <Item>
      <Key>ServerURLListUsers</Key>
      <Value>
        <LSOption>http://myurl/config.xml</LSOption>
        <LSOption>http://myurl</LSOption>
      </Value>
    </Item>
    <Item>
      <Key>UserDisplayDimensions</Key>
      <Value>fullscreen</Value>
    </Item>
  </User_Blob></root>'
($c | Select-Xml -XPath "//Item[Key = 'LogonMethod']").Node.Value
$c=[xml]'
logon方法
促使
ServerURLEntered
http://myserver/config.xml
ServerURLListUsers
http://myurl/config.xml
http://myurl
用户显示维度
全屏
'
($c | Select Xml-XPath”//Item[Key='LogonMethod'])

它更干净(如果你知道你在做什么)。

你也可以使用SelectSingleNode方法:

$xml.SelectSingleNode("//*[Key='LogonMethod']").Value

是否故意丢失了结束标记?@stej:很抱歉,这是一个错误,我不想发布超过需要的xml,但忘记了结束标记。(更正)$xml.root.User_Blob.Item.Key[“LogonMethod”]失败,无法索引到空数组中。可能是因为项目出现了不止一次。您的预期输出是什么?我本来希望有一种方法可以使用,而不必为每个/Where使用,但我认为这确实是唯一的方法。这对我来说很有用:($xml.root.User_Blob.Item | Where Object{$\u.Key-eq'LogonMethod'}).Value(所以不是Item.Value,只是Item),如果你更新你的答案,我会接受它。我没有得到任何输出,它只是我在PS中写的所有东西,最后添加了提示。+1但这对我来说更清楚:($c |选择xml-XPath”//Item[Key='LogonMethod']”).node.value+1但有一个问题,我们如何在使用Select XML时更新值?@Christian,您使用V2还是V3 CTP?
($c|Select XML-XPath)/Item[Key='LogonMethod']/value”).node.value
返回nothing@stejV2.和[xml]$c=gc file.xml。删除路径中的/值!!