且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Powershell:从 xml 文件中读取值

更新时间:2023-11-15 10:59:58

要获取 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...] }