I've got a xml log file. The file looks like this:
<Transaction name='0' id='1'>
<Response>Warning</Response>
<Statistic mode='Element'>
<Information>0</Information>
<Warning>0</Warning>
<Error>0</Error>
</Statistic>
<Messages>
<Message state='Warning'>Personal-Nr.: 12345, Tom Test</Message>
<Message state='Warning'>This is a warning message 1</Message>
<Message state='Warning'>This is a warning message 2</Message>
<Message state='Warning'>This is a warning message 3</Message>
<Message state='Warning'>This is a warning message 4</Message>
</Messages>
</Transaction>
This pattern repeats about 900 times Sometimes with more or less Messages. Now I just want to get all the Transactions where the <Response>Error</Response>
occurs.
So I made this code in Powershell:
## parsing xml file and opening inner node
Select-Xml -Path C:\Users\user\path\path\file.xml -XPath '/Paths/Task/Transaction' | ForEach-Object { $_.Node.InnerXML }
## looping through Response set with include="Error"
$_.Node.InnerXML | Where-Object Response -eq 'Error' | ForEach-Object { $_.Messages }
echo $_.Messages
But the only data I get is all of the transactions, no matter if the response is Warning
or Error
. Even further, it doesn't even matter if I only leave the Select-Xml
line and delete the rest. The result is always the same. I always get ALL of the responses.
So my question is:
How do I get only get the transactions where the Response is Error
?
Bonus question: Is there a possibility to just have the first message line of each Error
transaction as a output? So that I have a list of all the Personal-Nr
that were in an error transaction?
Thanks a lot