1

I have several XML file that I need to read and set variable data from the node values.

example XML:

<Objs Version="1.1.0.1" 
xmlns="http://schemas.microsoft.com/powershell/2004/04">
<Obj RefId="0">
<TN RefId="0">
  <T>System.__ComObject#{86fd1ebe-92e2-40f3-9c03-e5f0ca55f8ab}</T>
  <T>System.__ComObject</T>
  <T>System.MarshalByRefObject</T>
  <T>System.Object</T>
</TN>
<ToString>System.__ComObject</ToString>
<Props>
  <S N="Name">copy1234</S>
</Props>

The script I'm writing must read the file and get the value "copy1234" into a variable. I suck, though, so it just returns NULL...

    Get-ChildItem $ImportFolderPath -Filter *.xml | Foreach-Object {
    $currentFile = $_.FullName
    Write-Host "Processing file:   $currentfile"
    [xml]$ccXML = Get-content $currentFile


    $ccName = Select-Xml -Path $currentfile -Xpath '//objs/obj/props/S[@N="Name"]'

        Write-Host "Name of node:  $ccName"
3
  • 1
    Select-Xml -Path $currentfile -Xpath '/ns:Objs/ns:Obj/ns:Props/ns:S[@N="Name"]' -Namespace @{ns='http://schemas.microsoft.com/powershell/2004/04'} Commented Mar 12, 2018 at 22:30
  • Ok, this is a lot closer, but it now returns: value:FileFullPath I just need 'value'. I don't understand why it's joining the file name path. Commented Mar 12, 2018 at 22:40
  • You need to inspect what kind of object Select-Xml return to you. Commented Mar 12, 2018 at 22:51

1 Answer 1

1

You were missing the fact that XPath is case-sensitive.
So the expression should be

$ccName = Select-Xml -Path $currentfile -Xpath '//Objs/Obj/Props/S[@N="Name"]'

If Objs is your root element you could even simplify/optimize this by omitting the //(everywhere in the document) and using a /(relative to root element):

$ccName = Select-Xml -Path $currentfile -Xpath '/Objs/Obj/Props/S[@N="Name"]'
Sign up to request clarification or add additional context in comments.

1 Comment

Still returns NULL..... But, yes, you are right. I did forget the XPath casing. Thanks!

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.