【问题标题】:Can't extract value of XML nodes with powershell无法使用 powershell 提取 XML 节点的值
【发布时间】:2012-06-06 21:57:34
【问题描述】:

我正在尝试从 xml 文件中提取一些信息并根据需要更新/创建应用程序池。这是我正在阅读的 xml:

<?xml version="1.0" encoding="utf-8"?>
<appPool name="MyAppPool">
  <enable32BitAppOnWin64>true</enable32BitAppOnWin64>
  <managedPipelineMode>Integrated</managedPipelineMode>
  <managedRuntimeVersion>v4.0</managedRuntimeVersion>
  <autoStart>true</autoStart>
</appPool>

这就是我想要做的:

#read in the xml
$appPoolXml = [xml](Get-Content $file)

#get the name of the app pool
$name = $appPoolXml.appPool.name

#iterate over the nodes and update the app pool
$appPoolXml.appPool.ChildNodes | % {
     #this is where the problem exists
     set-itemproperty IIS:\AppPools\$name -name $_.Name -value $_.Value
}

$_.Name 返回正确的节点名称(即enable32BitAppOnWin64),但.Value 属性不返回任何内容。如何提取我想要的数据?

【问题讨论】:

    标签: xml powershell


    【解决方案1】:

    正确答案:

    你想要$_.'#text' 而不是$_.Value

    也考虑一下,它使用System.Xml.XmlElement 对象上的InnerText 属性:

    $xml.appPool.ChildNodes | % { $.Name; $.InnerText };

    【讨论】:

    • 太棒了!感谢您的帮助!
    • 其实正确的PowerShell应该是$appPoolXml.appPool.ChildNodes | % { $_.Name; $_.InnerText }
    • 请注意,InnerText 获取节点下的所有文本,因此 ([xml]"foobar").xml.InnerText 返回 " foob​​ar”,而您可能想要“foo”。(...)。 xml.'#text' 返回“foo”。
    猜你喜欢
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-01
    相关资源
    最近更新 更多