Powershell:从 xml 文件中读取值 [英] Powershell: Read value from xml files

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

问题描述

我需要一些有关 PowerShell 的帮助,请.应该很简单:

I need some with help with PowerShell, please. It should be pretty easy:

我有一个子目录列表,每个子目录中都有一个 xml 文件.我想打开每个 xml 文件并打印一个节点的值.节点始终相同,因为 xml 文件实际上是来自 Visual Studio 的项目文件 (*.csproj).

I have a list of subdirectories, with a xml file in each one. I want to open each xml file and print the value of one node. The node is always the same, as the xml files are actually project files (*.csproj) from Visual Studio.

我已经得到了文件列表:get-item ** \ *.csproj

I already got the list of files: get-item ** \ *.csproj

我该如何进行?

推荐答案

要获取 csproj 文件,请使用 Get-ChildItem

To get the csproj files use Get-ChildItem

Get-ChildItem c:\myProjects *.csproj -recurse

然后你可以使用例如Select-Xml 像这样:

Then you can use e.g. Select-Xml like this:

$ns = @{ defaultNamespace = "http://schemas.microsoft.com/developer/msbuild/2003" }
Get-ChildItem c:\myProjects *.csproj -recurse | 
   Select-Xml -xpath '//defaultNamespace:PropertyGroup[1]' -namespace $ns | 
   Select-Object -expand Node

您必须更正默认命名空间.我现在手头没有 csproj 文件.
(有关 PowerShell 中 xml 和 xpath 的更多信息,请参阅 http://huddledmasses.org/xpath-and-namespaces-in-powershell/)

You have to correct the default namespace. I have no csproj file by hand right now.
(for more info about xml and xpath in PowerShell see http://huddledmasses.org/xpath-and-namespaces-in-powershell/)

需要Select-Object来扩展实际的节点属性.

The Select-Object is needed to expand the actual node property.

如果你想像处理对象一样处理 xml,你可以使用这个:

If you would like to work with xml like with object, you can use this:

Get-ChildItem c:\myProjects *.csproj -recurse | 
   % { $content = [xml](gc $_.FullName); $content.Project.PropertyGroup[someindex...] }

这篇关于Powershell:从 xml 文件中读取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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