【发布时间】:2014-01-18 03:20:08
【问题描述】:
我发现很难找到从 XML 文件中检索内容的方法。下面是我的 xml 文件的样子。 我正在尝试检索完整的“nlog”节点。请帮忙。
<configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, ..."/>
</configSections>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<variable name="LoggingDirectory" value="D:/Logging/"/>
<include file="${LoggingDirectory}Config/Framework.nlog.xml"/>
</nlog>
</configuration>
这是我迄今为止尝试过的:
$nlogConfigFile = 'D:\machine.config.nlog.xml'
$nlogConfigXml = new-object xml
$nlogConfigXml.Load($nlogConfigFile);
$nlogConfigXml.PreserveWhitespace = $true
我已经使用了本博客http://blog.danskingdom.com/powershell-functions-to-get-an-xml-node-and-get-and-set-an-xml-elements-value-even-when-the-element-does-not-already-exist/提供的“Get-XmlNode”功能
Get-XmlNode -XmlDocument $nlogConfigXml -NodePath "configuration.configSections.section[@name='nlog']" ## works OK
Get-XmlNode -XmlDocument $nlogConfigXml -NodePath "configuration.nlog" ## does NOT work
我也尝试过 "Select-Xml" 、 .SelectSingleNode 命令,但它们似乎都不起作用。 如果我遗漏了什么,请告诉我。
【问题讨论】:
标签: xml powershell powershell-ise