Loop through XML document nodes and get data

Tiwaz
Member Posts: 98
Hello experts.
I have an XML document which has a structure something like in the picture.

Lets assume I have two tables: Table1 and Table2.
I need to insert record from <Data> to Table 1 and from <DataChild> to Table 2.
I need to go through all <DataChild> nodes and insert data from them to designated fields in Table 2, in other words,
create new record for each occurence of <DataChild>.
How can this be done using DotNet in NAV?
THANK YOU
I have an XML document which has a structure something like in the picture.

Lets assume I have two tables: Table1 and Table2.
I need to insert record from <Data> to Table 1 and from <DataChild> to Table 2.
I need to go through all <DataChild> nodes and insert data from them to designated fields in Table 2, in other words,
create new record for each occurence of <DataChild>.
How can this be done using DotNet in NAV?
THANK YOU

0
Best Answers
-
Convert that to a XSD in VS and use the xmlport generator that is in Mibuso6
-
Hey,
this is a really quick and dirty example how you can read you XML.
XML:<?xml version="1.0" encoding="utf-8"?> <Data> <String1>Test1</String1> <String2>Test2</String2> <DataChild> <String3>Test10</String3> <String4>Test20</String4> </DataChild> <DataChild> <String3>Test30</String3> <String4>Test40</String4> </DataChild> </Data>
Variables:Doc DotNet System.Xml.XmlDocument.'System.Xml ParrentNode DotNet System.Xml.XmlNode.'System.Xml ChildNode DotNet System.Xml.XmlNode.'System.Xml NodeList DotNet System.Xml.XmlNodeList.'System.Xml ChildNodeList DotNet System.Xml.XmlNodeList.'System.Xml i Integer j Integer
Doc := Doc.XmlDocument(); Doc.Load('V:\NAV\XMLExample\MibusoXML.xml'); ParrentNode := Doc.SelectSingleNode('Data'); NodeList := ParrentNode.ChildNodes(); // NodeList ... String1, String2, DataChild, DataChild FOR i := 0 TO NodeList.Count -1 DO BEGIN ChildNode := NodeList.ItemOf(i); CASE ChildNode.Name OF 'DataChild': BEGIN ChildNodeList := ChildNode.ChildNodes(); FOR j := 0 TO ChildNodeList.Count - 1 DO BEGIN ChildNode := ChildNodeList.ItemOf(j); MESSAGE('I''''m a DataChild Node Name: %1 InnerText: %2',ChildNode.Name,ChildNode.InnerText); END; END; ELSE BEGIN MESSAGE('I''''m a Data Node Name: %1 InnerText: %2',ChildNode.Name,ChildNode.InnerText); END; END; END;
If you invest more time in this you will see that you can create a code which is much more flexible or maybe also recursive. There are a lot examples in c# which you can convert to c/al.
5
Answers
-
Convert that to a XSD in VS and use the xmlport generator that is in Mibuso6
-
Hey,
this is a really quick and dirty example how you can read you XML.
XML:<?xml version="1.0" encoding="utf-8"?> <Data> <String1>Test1</String1> <String2>Test2</String2> <DataChild> <String3>Test10</String3> <String4>Test20</String4> </DataChild> <DataChild> <String3>Test30</String3> <String4>Test40</String4> </DataChild> </Data>
Variables:Doc DotNet System.Xml.XmlDocument.'System.Xml ParrentNode DotNet System.Xml.XmlNode.'System.Xml ChildNode DotNet System.Xml.XmlNode.'System.Xml NodeList DotNet System.Xml.XmlNodeList.'System.Xml ChildNodeList DotNet System.Xml.XmlNodeList.'System.Xml i Integer j Integer
Doc := Doc.XmlDocument(); Doc.Load('V:\NAV\XMLExample\MibusoXML.xml'); ParrentNode := Doc.SelectSingleNode('Data'); NodeList := ParrentNode.ChildNodes(); // NodeList ... String1, String2, DataChild, DataChild FOR i := 0 TO NodeList.Count -1 DO BEGIN ChildNode := NodeList.ItemOf(i); CASE ChildNode.Name OF 'DataChild': BEGIN ChildNodeList := ChildNode.ChildNodes(); FOR j := 0 TO ChildNodeList.Count - 1 DO BEGIN ChildNode := ChildNodeList.ItemOf(j); MESSAGE('I''''m a DataChild Node Name: %1 InnerText: %2',ChildNode.Name,ChildNode.InnerText); END; END; ELSE BEGIN MESSAGE('I''''m a Data Node Name: %1 InnerText: %2',ChildNode.Name,ChildNode.InnerText); END; END; END;
If you invest more time in this you will see that you can create a code which is much more flexible or maybe also recursive. There are a lot examples in c# which you can convert to c/al.
5
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions