0

Some XML files I'm looking at don't always include the name but

</NAME>

If I have trim() in the code it fails

[PSCustomObject]@{
   Title = $customer.Name.Title.Trim()
   FirstName = $customer.Name.First_Name.Trim()
   LastName = $customer.Name.Last_Name.Trim()
   EmailConsent = $emailConsent
   EmailSoftOptIn = $emailSoftOptIn
}

How can I check so not apply trim() when attribute isn't there. I'm just researching to to see if I can assign to a variable and then check if it's empty but is this the best way.

This is linked to a previous post which was answered perfectly (I have put here for insight to full code)

Powershell access XML node with multiple attributes within a forloop

5
  • 1
    then check for null first Commented Oct 16, 2020 at 13:53
  • 1
    $customer.Name.Title -as [string] |% Trim Commented Oct 16, 2020 at 18:36
  • Thanks Mathias I was looking into using null as marsze suggested but that one above looks cleaner than my attempt Commented Oct 17, 2020 at 9:44
  • $Title = $customer.Name.Title if($Title -ne $null) {$Title=$Title.Trim()} Commented Oct 17, 2020 at 9:44
  • 1
    Or use ('' + $customer.Name.Title).Trim() or regex: $customer.Name.Title -replace '^\s+|\s+$' Commented Oct 17, 2020 at 13:41

0

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.