0

I have the following XML, and I'm trying to obtain the value where the Name attribute is ComponentVersion.

<Component .......>
  <Settings>
    <Setting Name="ComponentVersion" Value="12345" />
    <Setting Name="Host" Value="false" />
  </Settings>
  <Jobs>
    <Job Name="Platform" />
  </Jobs>
</Component>

I read the XML file like this:

$fullPathXML = "$env:PackagesPath\Platform\Components\SystemPlatform.Comp.xml"
[xml]$xmlSystemPlatform = Get-Content $fullPathXML

and then tried the following:

  1. $xmlSystemPlatform.selectNodes('//Settings/Setting') | select Name

  2. Select-Xml '//Settings/Setting[contains(@Name, "ComponentVersion")]' $xmlSystemPlatform |%{$_.Node.Value}

  3. $xmlSystemPlatform.Settings.Setting[0].Name $xmlSystemPlatform.Settings.Setting[0].Value

3 Answers 3

2
$xmlSystemPlatform.Component.Settings.Setting | ? Name -eq "ComponentVersion" | select Value
Sign up to request clarification or add additional context in comments.

1 Comment

Note that the simplified filter syntax requires PowerShell v3 or newer. For earlier versions you need ? { $_.Name -eq "ComponentVersion" }.
1

Using an XPath expression something like this should work:

$filename = "$env:PackagesPath\Platform\Components\SystemPlatform.Comp.xml"

[xml]$xml = Get-Content $filename

$xml.SelectNodes('//Settings/Setting[@Name = "ComponentVersion"]').Value

Note that XPath expressions are case-sensitive.

Comments

0

Another one-liner:

([Xml] (Get-Content "$env:PackagesPath\Platform\Components\SystemPlatform.Comp.xml")).SelectNodes("//Settings/Setting[@Name='ComponentVersion']").Value

Or if you prefer it with variable:

$xmlSystemPlatform.SelectNodes("//Settings/Setting[@Name='ComponentVersion']").Value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.