Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Answered

How to import data to BC from XML?

(0) ShareShare
ReportReport
Posted on by 93

Hi, 

Currently I have a procedure that reads and imports some elements from the XML to a table correctly, I only need to read some elements well, since some fields are repeated when they are imported.

I get the XML of a response from a WS, the response is stored in InStream and then I read it to import the information into a table, this is part of the code.

if Client.Send(RequestMessage, ResponseMessage) then begin
            ResponseMessage.Content().ReadAs(InStrmResponse);
            Data.SetCurrentKey(TempLineNo);
            if Data.Find(' ') then
                TempLineNo := Data.TempLineNo;
            
            if XmlDocument.ReadFrom(InStrmResponse, xmlDoc) then begin
                xmlnsMgr.AddNamespace('ns', 'http://www.company.com.br');
                if xmlDoc.SelectNodes('//ns:Movimentacao', xmlnsMgr, xmlNodList) then begin
                    foreach xmlNodRoot in xmlNodList do begin
                        xmlNodRoot.SelectSingleNode('ns:Codigo', xmlnsMgr, xmlNod1);
                        xmlNodRoot.SelectSingleNode('ns:TipoMovimentacao', xmlnsMgr, xmlNod2);
                        xmlNodRoot.SelectSingleNode('ns:DataMovimentacao', xmlnsMgr, xmlNod3);
                        xmlNodRoot.SelectSingleNode('ns:EmpresaOrigem/ns:IDUnihealth', xmlnsMgr, xmlNod4);
                        xmlNodRoot.SelectSingleNode('ns:CentroCustoOrigem/ns:IDUnihealth', xmlnsMgr, xmlNod5);
                        xmlNodRoot.SelectSingleNode('//ns:CentroCustoOrigem/ns:CodigoExterno', xmlnsMgr, xmlNod6);
                        xmlNodRoot.SelectSingleNode('ns:EmpresaDestino/ns:IDUnihealth', xmlnsMgr, xmlNod7);
                        xmlNodRoot.SelectSingleNode('ns:CentroCustoDestino/ns:IDUnihealth', xmlnsMgr, xmlNod8);
                        xmlNodRoot.SelectSingleNode('//ns:CentroCustoDestino/ns:CodigoExterno', xmlnsMgr, xmlNod9);
                        xmlNodRoot.SelectSingleNode('ns:TipoDocumento', xmlnsMgr, xmlNod10);
                        xmlNodRoot.SelectSingleNode('//ns:CodigoDocumento', xmlnsMgr, xmlNod11);
                        xmlNodRoot.SelectSingleNode('ns:Produto/ns:IDUnihealth', xmlnsMgr, xmlNod12);
                        xmlNodRoot.SelectSingleNode('ns:Produto/ns:CodigoExterno', xmlnsMgr, xmlNod13);
                        xmlNodRoot.SelectSingleNode('//ns:CodigoProdutoSecundario', xmlnsMgr, xmlNod14);
                        xmlNodRoot.SelectSingleNode('//ns:CodigoEmbalagemProduto', xmlnsMgr, xmlNod15);
                        xmlNodRoot.SelectSingleNode('ns:Quantidade', xmlnsMgr, xmlNod16);
                        xmlNodRoot.SelectSingleNode('ns:Valor', xmlnsMgr, xmlNod17);
                        xmlNodRoot.SelectSingleNode('//ns:Lote', xmlnsMgr, xmlNod18);
                        xmlNodRoot.SelectSingleNode('ns:Validade', xmlnsMgr, xmlNod19);
                        xmlNodRoot.SelectSingleNode('ns:CodigoItemDocumento', xmlnsMgr, xmlNod20);
                        xmlNodRoot.SelectSingleNode('ns:Usuario', xmlnsMgr, xmlNod21);
                        xmlNodRoot.SelectSingleNode('ns:CodigoEtiqueta', xmlnsMgr, xmlNod22);
                        xmlNodRoot.SelectSingleNode('ns:Identificador', xmlnsMgr, xmlNod23);

                        TempLineNo := TempLineNo   1;
                        Data.TempLineNo := TempLineNo;
                        Data.Init();
                        Data.Insert(true);

                        Data.Codigo := (xmlNod1.AsXmlElement().InnerXml);
                        Data.TipoMovimentacao := (xmlNod2.AsXmlElement().InnerText);

                        txtDate := (xmlNod3.AsXmlElement().InnerText);
                        txtNewDate := CopyStr(txtDate, 1, 4)
                          '-'   CopyStr(txtDate, 6, 2)
                          '-'   CopyStr(txtDate, 9, 2)
                          ' '   CopyStr(txtDate, 12, 2)
                          ':'   CopyStr(txtDate, 15, 2)
                          ':'   CopyStr(txtDate, 18, 2);
                        Evaluate(formatDate, txtNewDate);
                        Data.DateMovimentacao := formatDate;

                        Data.EmpresaOrigemIDUnihealth := (xmlNod4.AsXmlElement().InnerText);
                        Data.CentroCustoOrigemIDUnihealth := (xmlNod5.AsXmlElement().InnerText);
                        Data.CentroCustoOrigemCodExterno := (xmlNod6.AsXmlElement().InnerText);
                        Data.EmpresaDestinoIDUnihealth := (xmlNod7.AsXmlElement().InnerText);
                        Data.CentroCustDestinoIDUnihealth := (xmlNod8.AsXmlElement().InnerText);
                        Data.CentroCustDestinoIDCodExterno := (xmlNod9.AsXmlElement().InnerText);
                        Data.TipoDocumento := (xmlNod10.AsXmlElement().InnerText);
                        Data.CodigoDocumento := (xmlNod11.AsXmlElement().InnerText);
                        Data.ProductoIDUnihealth := (xmlNod12.AsXmlElement().InnerText);
                        Data.ProductoCodigoExterno := (xmlNod13.AsXmlElement().InnerText);
                        Data.CodigoProdutoSecundario := (xmlNod14.AsXmlElement().InnerText);
                        Data.CodigoEmbalagemProduto := (xmlNod15.AsXmlElement().InnerText);
                        Data.Quantidade := (xmlNod16.AsXmlElement().InnerText);
                        Data.Valor := (xmlNod17.AsXmlElement().InnerText);
                        Data.Lote := (xmlNod18.AsXmlElement().InnerText);
                        Data.Validade := (xmlNod19.AsXmlElement().InnerText);
                        Data.CodigoItemDocumento := (xmlNod20.AsXmlElement().InnerText);
                        Data.Usuario := (xmlNod21.AsXmlElement().InnerText);
                        Data.CodigoEtiqueta := (xmlNod22.AsXmlElement().InnerText);
                        Data.Identificador := (xmlNod23.AsXmlElement().InnerText);
                        Data.Modify(true);
                    end;
                end;
            end;

So that I can read some elements I had to add a double slash (//) since if I didn't I would see an error similar to this: "The XPath query 'ns:Lote' does not match any nodes", with the code above I I'm not getting that error and the records import apparently fine, but repeated. What should I do so that the records are not repeated in cases that must start with a double bar or what should I do to avoid putting them?

This is the structure of the XML

xye9in4mqa0d.jpg

Categories:
  • Verified answer
    isabtogumon Profile Picture
    93 on at
    RE: How to import data to BC from XML?

    Thank you Marco

  • Suggested answer
    Marco Mels Profile Picture
    on at
    RE: How to import data to BC from XML?

    Hello,

    We currently do not have dedicated Dev support via the Dynamics 365 Business Central forums, but I wanted to provide you some additional resources to assist.  If you need assistance with debugging or coding I would recommend discussing this on one of our communities.

    www.yammer.com/dynamicsnavdev

    dynamicsuser.net/.../developers

    I will open this up to the community in case they have something to add.

    Thanks.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Dynamics 365 Community Update – Sep 16th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard > Small and medium business | Business Central, NAV, RMS

Featured topics

Product updates

Dynamics 365 release plans