PowerShell中操作XML文件问题 [英] PowerShell manipulating XML file issue

查看:554
本文介绍了PowerShell中操作XML文件问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下的XML文件,我想使用PowerShell(版本1.0)来操作XML文件以获取富价值(在此示例中,值我想要得到的是富价值)和咕(在此示例中,值我想要得到的是咕值),任何想法如何实现?

Suppose I have the following XML file, and I want to use PowerShell (version 1.0) to manipulate the XML file to get the value for Foo (in this sample, value I want to get is "Foo Value") and Goo (in this sample, value I want to get is "Goo Value"), any ideas how to implement?

$FooConfig = [xml](get-content .\Foo.exe.config -ErrorAction:stop)

<configuration>
 <appSettings>
    <add key="Foo" value="Foo Value" />
    <add key="Goo" value="Goo Value" />
 </appSettings>
</configuration>

在此先感谢, 乔治

thanks in advance, George

推荐答案

中的XPath API是PowerShell的下很有活力(你的XML是,毕竟只是.NET对象),它往往是最简单的,如果你使用只想值:

The XPath API is very alive under PowerShell (your XML is, after all, just .NET objects), and it can often be the easiest to use if you just want a value:

$appSettingsSection = $fooConfig.configuration.appSettings;
$goo = $appSettingsSection.SelectSingleNode("add[@key='Goo']");
$goo.Value

或者,如果你想枚举添加元素为PowerShell的集合(多一点PowerShell'ish :-)

Or if you want to enumerate the add elements as a PowerShell collection (a bit more PowerShell'ish :-)

$appSettingsSection = $fooConfig.configuration.appSettings.add

将打印

key                                                         value
---                                                         -----
Foo                                                         Foo Value
Goo                                                         Goo Value

当然,可以通过管道将导致额外的命令做任何处理,你喜欢的。

And of course, you can pipe that result to additional commands to do whatever processing you like.

这篇关于PowerShell中操作XML文件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆