给定此示例XML数据,是否可以直接访问Key?

例如:$ xml.root.User_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>

最佳答案

试试这个:-

[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()

08-06 08:02