【问题标题】:Powershell script using xPath .SelectSingleNode not working on extracting values from web.config file xmlns使用 xPath .SelectSingleNode 的 Powershell 脚本无法从 web.config 文件 xmlns 中提取值
【发布时间】:2020-11-21 12:28:18
【问题描述】:

我的 xml 与下面的普通 xml 略有不同,xmlns 不在根级别,它是从根开始的 2 级深度,我无法使用以下解决方案。谁能帮我选择节点。提前致谢。

我无法使用下面的 sn-p 来选择节点

$ns = New-Object System.Xml.XmlNamespaceManager($WebConfigXml.NameTable)
$ns.AddNamespace("ns", $WebConfigXml.DocumentElement.NamespaceURI)
$node = $WebConfigXml.SelectSingleNode("//ns:add[@key='SiteDomain']", $ns)

XML:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<app1>
</app1>
<assembly>
<runtime xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<location path="." inheritInChildApplications="false">
<appSettings>
<!--IT Ops-->
<add key="SomeOtherKey" value="SomeOtherValue" />
<add key="SiteDomain" value="somedomain.com" />
<add key="SomeOtherKey" value="SomeOtherValue" />
....
</appSettings>
</location>
</runtime>
</assembly>
</configuration>

【问题讨论】:

    标签: powershell namespaces xml-namespaces selectsinglenode


    【解决方案1】:

    您的命名空间未在 $ns 中正确初始化。您需要深入到包含命名空间定义的节点:

    $ns.AddNamespace('ns',$WebConfigXml.configuration.assembly.runtime.NamespaceURI) 
    

    【讨论】:

    • 非常感谢 AdminOfThings,这为我节省了很多时间。我没有找到任何例子:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多