Powershell:从xml文件中读取值

Dan*_*elR 4 powershell

请给我一些帮助PowerShell的帮助.它应该很简单:

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

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

我该怎么办?

ste*_*tej 6

要使用csproj文件 Get-ChildItem

Get-ChildItem c:\myProjects *.csproj -recurse
Run Code Online (Sandbox Code Playgroud)

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

$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
Run Code Online (Sandbox Code Playgroud)

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

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

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

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