web
You’re offline. This is a read only version of the page.
close
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 99

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

I have the same question (0)
  • Suggested answer
    Marco Mels Profile Picture
    on at

    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.

  • Verified answer
    isabtogumon Profile Picture
    99 on at

    Thank you Marco

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 3,229

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 1,867 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,153 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans