Accessing Objects when using XML

Hi everybody,

this is my first question (I hope not too stupid…) in this forum. I have looked for a solution here and in the web, but did not find anything.

When I create an object from an XML file like

$Main = [xml] (Get-Content -Path ($Source + $XMLMainDocument))

I can access the XML File like this

$BZRFromXML = $Main.archive.content.document.extension.property.value[0]

And here is my problem:

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.

Can you help me?

Thanks for your time

You can cast a single element to an array if needed … example:

$Variable = ‘singleElement’
$Variable[0]
@($Variable)[0]

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?

Thanks again.

In case you create an array with a sinlge element in it.

Try it.

I don’t understand this question.

Sorry, bad question…

I have code like this :

for ($k = 0; $k -le ($MyRecordsCount -1); $k++)
{
$date = $Staple.LedgerImport.consolidate.cashLedger.date[$k]
$amount = $Staple.LedgerImport.consolidate.cashLedger.amount[$k]
$accountNo = $Staple.LedgerImport.consolidate.cashLedger.accountNo[$k]
$buCode = $Staple.LedgerImport.consolidate.cashLedger.buCode[$k]
#DoSomething

}

$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

{
$date = $Staple.LedgerImport.consolidate.cashLedger.date
$amount = $Staple.LedgerImport.consolidate.cashLedger.amount
$accountNo = $Staple.LedgerImport.consolidate.cashLedger.accountNo
$buCode = $Staple.LedgerImport.consolidate.cashLedger.buCode
#DoSomething

}

I have the feeling that this is not a smart way to do programming :wink:

But I also have not found out how to apply your first answer to solve my problem.

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.

To use your example … that could be one way:

for ($k = 0; $k -le ($MyRecordsCount - 1); $k++) {
    $date = @($Staple.LedgerImport.consolidate.cashLedger.date)[$k]
    $amount = @($Staple.LedgerImport.consolidate.cashLedger.amount)[$k]
    $accountNo = @($Staple.LedgerImport.consolidate.cashLedger.accountNo)[$k]
    $buCode = @($Staple.LedgerImport.consolidate.cashLedger.buCode)[$k]
    #DoSomething
}

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)'"
}

Now I got the picture. Thank you very much!

Thank you also for the hint with foreach. I know it and use it in my software, but in this case, I need $k in the DoSomething-section anyway.

No doubt that your rank is Community Hero :wink: :slight_smile: