Get value from xml files
Hi everyone
I recently started developing extensions for Microsoft Dynamics NAV in Visual Studio Code. My latest assignment is to read data from xml files and insert into certain tables.
I am trying out the XMLDocument Class and I can successfully load the file and navigate to any node. However I'm having trouble getting the value of a certain node.
For example if I have the following xml file:
<XmlFile>
<People>
<Person>
<FirstName>John</FirstName>
<LastName>Doe</LastName>
</Person>
<Person>
<FirstName>Jane</FirstName>
<LastName>Doe</LastName>
</Person>
</People>
</XmlFile>
How would I get the value of FirstName and LastName for each person using XMLDocument and related Classes?
Hi
You can try the below code. I have not tested this but it should work.
Variables:
lXmlDocument : XmlDocument; lPersonXmlNode: XmlNode; lText: Text; lXmlNode: XMLNode; lXmlNodeList: XMLNodeList;
Code:
XmlDocument.ReadFrom(lText, lXmlDocument);
IF lXmlDocument.SelectNodes('//People/Person',lXmlNodeList) then begin
foreach lPersonXmlNode in lXmlNodeList do
begin
IF lPersonXmlNode.SelectSingleNode('FirstName', lXmlNode) then
FirstName := lXmlNode.AsXmlElement.InnerText;
IF lPersonXmlNode.SelectSingleNode('LastName', lXmlNode) then
LastName := lXmlNode.AsXmlElement.InnerText;
end;
end;
Please let me know your comments.
Thank you very much!
Using the lXmlNode.AsXmlElement.InnerText; works in my project.