If there is no such node in the object, I do not get a valid value. No Problem. If there are several ‘value’-nodes, I can access them as an array. No problem either. But if there is exactly one ‘value’, my code using the array notation does not work, because it is a single object, not an array. Thus, using results in an error.
I have circumvented the problem by writing two code branches for no/several and one value. That is a lot of work and bad for maintenance. I am sure, there is a better way, I just don’t know it.
OK, I just create an array from my single variable. Can I do that in a complex object like in my example? And how do I address the fact that I simply don’t know if my variable is single or array?
$Staple is an object created from an XML file. If $MyRecordsCount is exactly 1, this Code does not work, because none of the above objects is an array. Therefore, right now I make a second branch by asking if $MyRecordsCount is 1 where the code is
When you post code or error messages or sample data or console output format it as code, please.
In the “Text” view you can use the code tags “PRE”, in the “Visual” view you can use the format template “Preformatted”.
Thanks in advance.
I hope you are aware of the fact that you do not need this “complex” loop. In Powershell we have the foreach loop. I don’t know the structure of your XML but when you have an array of elements you can easily iterate over the elements of that array with foreach like this:
$arrayOfElements = 'Eins','Zwei','Drei','Vier'
foreach($SingleElement in $arrayOfElements){
"Do something with '$($SingleElement)'"
}