Powershell: Using a variable inside SelectNodes does not work

228 views Asked by At

I want to use a variable in the SelectNodes command. Unfortunately it does not work :( The content of the XML is in german, sorry for this.

    Write-Output $propName
    $propertyValue= $inputFile.SelectNodes("/MatML_Doc/Material/BulkDetails/PropertyData[@property='$propName']/Data")

Write-Output gives me the string 'Bruchdehnung'. But $propertyValue is empty.

When I replace $propName in the SelectNodes command with 'Bruchdehnung' it works as expected.

$propertyValue= $inputFile.SelectNodes("/MatML_Doc/Material/BulkDetails/PropertyData[@property='Bruchdehnung']/Data")

Here is the XML File:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<MatML_Doc>

  <Material>
    <BulkDetails>
      <Notes/>
      <Name>1.0037 (St37)</Name>
      <Class>
        <Name>METAL|Steel</Name>
      </Class>
      <Source source=""/>
      <ExternalIdentifier>1.0037 (St37)</ExternalIdentifier>
      <PropertyData property="Bruchdehnung">
        <Data format="exponential">0.000000e+000</Data>
      </PropertyData>
      <PropertyData property="Dichte">
        <Data format="exponential">7.800000e+003</Data>
      </PropertyData>
      <PropertyData property="Elastizitätsmodul">
        <Data format="exponential">2.100000e+011</Data>
      </PropertyData>
      <PropertyData property="Material Type">
        <Data format="string">IsotropicMaterial</Data>
      </PropertyData>
      <PropertyData property="Poissonscher Beiwert">
        <Data format="exponential">2.800000e-001</Data>
      </PropertyData>
      <PropertyData property="Spezifische Wärme">
        <Data format="exponential">4.400000e+002</Data>
      </PropertyData>
      <PropertyData property="Streckgrenze">
        <Data format="exponential">2.350000e+008</Data>
      </PropertyData>
      <PropertyData property="Wärmeausdehnungskoef.">
        <Data format="exponential">1.100000e-005</Data>
      </PropertyData>
      <PropertyData property="Wärmeleitfähigkeit">
        <Data format="exponential">1.400000e+001</Data>
      </PropertyData>
      <PropertyData property="Zugfestigkeit">
        <Data format="exponential">3.600000e+008</Data>
      </PropertyData>
    </BulkDetails>
  </Material>

Do you have any idea why I cant use the variable inside the SelectNodes command?

1

There are 1 answers

0
Elm On

Thanks everyone for your help! I found the solution:

$propertyValue= $inputFile.SelectNodes("/MatML_Doc/Material/BulkDetails/PropertyData[@property='$propName.Value']/Data")

I can not read directly the XML object. I have to use .Value to get the actual value :)