AL icon indicating copy to clipboard operation
AL copied to clipboard

Get value from xml files

Open hermannsson opened this issue 8 years ago • 2 comments

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?

hermannsson avatar Mar 01 '18 19:03 hermannsson

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.

diveshbora avatar Mar 02 '18 05:03 diveshbora

Thank you very much!

Using the lXmlNode.AsXmlElement.InnerText; works in my project.

hermannsson avatar Mar 02 '18 09:03 hermannsson