Powershell: reading values ​​from xml files

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

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

I already have a list of files: get-item ** \ *. Csproj

How do I proceed?

+4
source share
1 answer

To get csproj files, use Get-ChildItem

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

Then you can use for example. Select-Xml as follows:

 $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 must fix the default namespace. Right now I do not have a csproj file.
(for more information on xml and xpath in PowerShell, see http://huddledmasses.org/xpath-and-namespaces-in-powershell/ )

Extending the actual node property requires Select-Object .

If you want to work with xml as an object, you can use this:

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

Source: https://habr.com/ru/post/1304775/


All Articles