cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
john_doe
Frequent Visitor

XML iterate through nodes and get values

Hi!

 

I'm trying to loop over xml node and read each value in a structure like: (the actual structure is more complexe)

 

 

<main>
<records>
<year>2022</year>
</records>
<records>
<year>2021</year>
</records>
<records>
<year>2020</year>
</records>
</main>

 

 How can I perform this in a for each? in order to read each value.

I ve tried for each XpathQueryResult to get the <records> and after that CurrentItem.children[0].value - but is not possible. 

Please help!

1 ACCEPTED SOLUTION

Accepted Solutions
Henrik_M
Super User
Super User

SET Text TO $'''<main>
<records>
<year>2022</year>
</records>
<records>
<year>2021</year>
</records>
<records>
<year>2020</year>
</records>
</main>'''
File.GetTempPath TempFile=> TempFile
File.WriteText File: TempFile TextToWrite: Text AppendNewLine: True IfFileExists: File.IfFileExists.Overwrite Encoding: File.FileEncoding.DefaultEncoding
XML.ReadFromFile File: TempFile Encoding: XML.FileEncoding.DefaultEncoding XmlDocument=> XmlDocument
XML.ExecuteXPathQuery.ExecuteXPath XmlDocument: XmlDocument XPathQuery: $'''main/records''' XPathResults=> XPathResults
LOOP FOREACH CurrentItem IN XPathResults
    XML.ExecuteXPathQuery.ExecuteXPath XmlDocument: CurrentItem XPathQuery: $'''records/year/text()''' XPathResults=> Year
    Display.ShowMessageDialog.ShowMessage Message: Year Icon: Display.Icon.None Buttons: Display.Buttons.OK DefaultButton: Display.DefaultButton.Button1 IsTopMost: True
END

View solution in original post

3 REPLIES 3
Henrik_M
Super User
Super User

 

SET Text TO $'''<main>
<records>
<year>2022</year>
</records>
<records>
<year>2021</year>
</records>
<records>
<year>2020</year>
</records>
</main>'''
File.GetTempPath TempFile=> TempFile
File.WriteText File: TempFile TextToWrite: Text AppendNewLine: False IfFileExists: File.IfFileExists.Overwrite Encoding: File.FileEncoding.DefaultEncoding
XML.ReadFromFile File: TempFile Encoding: XML.FileEncoding.DefaultEncoding XmlDocument=> XmlDocument
XML.ExecuteXPathQuery.ExecuteXPath XmlDocument: XmlDocument XPathQuery: $'''main/records/year/text()''' XPathResults=> XPathResults
LOOP FOREACH CurrentItem IN XPathResults
    Display.ShowMessageDialog.ShowMessage Message: CurrentItem Icon: Display.Icon.None Buttons: Display.Buttons.OK DefaultButton: Display.DefaultButton.Button1 IsTopMost: True
END

Paste this code into PAD to see how.

 

Nice trick! it works perfectly.

The node <Records> holds about 9 values: year, month, city ....

I was hopping to extract with Xpath the Record node, and call each property on current Item like ... CurrentItem[Year].Value, CurrentItem[Month].Value or by index. 

I Find Your solution limited to Year children. How it can be addapted to all values of <Record>?  

Henrik_M
Super User
Super User

SET Text TO $'''<main>
<records>
<year>2022</year>
</records>
<records>
<year>2021</year>
</records>
<records>
<year>2020</year>
</records>
</main>'''
File.GetTempPath TempFile=> TempFile
File.WriteText File: TempFile TextToWrite: Text AppendNewLine: True IfFileExists: File.IfFileExists.Overwrite Encoding: File.FileEncoding.DefaultEncoding
XML.ReadFromFile File: TempFile Encoding: XML.FileEncoding.DefaultEncoding XmlDocument=> XmlDocument
XML.ExecuteXPathQuery.ExecuteXPath XmlDocument: XmlDocument XPathQuery: $'''main/records''' XPathResults=> XPathResults
LOOP FOREACH CurrentItem IN XPathResults
    XML.ExecuteXPathQuery.ExecuteXPath XmlDocument: CurrentItem XPathQuery: $'''records/year/text()''' XPathResults=> Year
    Display.ShowMessageDialog.ShowMessage Message: Year Icon: Display.Icon.None Buttons: Display.Buttons.OK DefaultButton: Display.DefaultButton.Button1 IsTopMost: True
END

Helpful resources

Announcements
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

New Ideas Forum MPA.jpg

A new place to submit your Ideas for Power Automate

Announcing a new way to share your feedback with the Power Automate Team.

MPA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

Learn to digitize and optimize business processes and connect all your applications to share data in real time.

365 EduCon 768x460.png

Microsoft 365 EduCon

Join us for two optional days of workshops and a 3-day conference, you can choose from over 130 sessions in multiple tracks and 25 workshops.

Top Solution Authors
Top Kudoed Authors
Users online (4,889)