直接访问XML中的键值对

2023-12-25

给定这个示例 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>

就我个人而言,当Where-Object需要的话,我会用Select-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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

直接访问XML中的键值对 的相关文章

随机推荐