开发者

Powershell: Read value from xml files

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

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.

I already got the l开发者_高级运维ist of files: get-item ** \ *.csproj

How do I proceed?


To get the csproj files use Get-ChildItem

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

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

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/)

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

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...] }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜