Skip to main content

Notifications

Community site session details

Community site session details

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

How to Access XML Nodes and Retrieve Attribute Values After Loading XML into InStream Variable?

(1) ShareShare
ReportReport
Posted on by 99
Hi,
 
In the context of an AL extension for Dynamics 365 Business Central, I have a scenario where I need to select an XML file using the code provided below and then parse its nodes to extract attribute values. Here's the code snippet for loading the XML into an InStream variable:
 
if UploadIntoStream(DialogTitle, '', 'XML file(*.xml)|*.xml', TempFileName, InStrm) then    XmlDocument.ReadFrom(InStrm, xmlDoc)else    Error('It is not an XML file');
The uploaded XML file structure is as follows:
 
<cfdi:Comprobante xmlns:cfdi=/http://www.sat.gob.mx/cfd/4/ xmlns:xsi=/http://www.w3.org/2001/XMLSchema-instance/ Certificado=/FICTITIOUS_CERTIFICATE/ PaymentConditions=/30/ Export=/01/ Date=/2023-08-15T10:30:45/ Folio=/12345/ PaymentMethod=/01/ ExpeditionLocation=/75000/ PaymentType=/PUE/ Currency=/USD/ CertificateNumber=/SAT_CERTIFICATE/ Seal=/FICTITIOUS_DIGITAL_SEAL/ Series=/B/ Subtotal=/500.00/ ExchangeRate=/1.00/ DocumentType=/I/ Total=/575.00/ Version=/3.3/ xsi:schemaLocation=/http://www.sat.gob.mx/cfd/4 http://www.sat.gob.mx/sitio_internet/cfd/4/cfdv33.xsd/>  <cfdi:Issuer Name=/FICTITIOUS_ISSUER/ FiscalRegime=/601/ Rfc=/FICTITIOUS_ISSUER_RFC/ />  <cfdi:Receiver FiscalAddress=/76000/ Name=/FICTITIOUS_RECEIVER/ FiscalRegimeReceiver=/612/ Rfc=/FICTITIOUS_RECEIVER_RFC/ CfdiUsage=/G01/ />  <cfdi:Concepts>    <cfdi:Concept Quantity=/10/ ProdServKey=/01010101/ UnitKey=/H87/ Description=/Sample Product/ Amount=/500.00/ IdentificationNumber=/PROD-001/ Unit=/Piece/ UnitValue=/50.00/>      <cfdi:Taxes>        <cfdi:Transfers>          <cfdi:Transfer Base=/500.00/ Amount=/80.00/ Tax=/002/ RateOrQuota=/0.160000/ TaxType=/Rate/ />        </cfdi:Transfers>      </cfdi:Taxes>      <cfdi:CustomsInformation PedimentNumber=/15 12345 67890 1234567/ />    </cfdi:Concept>  </cfdi:Concepts>  <cfdi:Taxes TotalTransferredTaxes=/80.00/>    <cfdi:Transfers>      <cfdi:Transfer Base=/500.00/ Amount=/80.00/ Tax=/002/ RateOrQuota=/0.160000/ TaxType=/Rate/ />    </cfdi:Transfers>  </cfdi:Taxes>  <cfdi:Complement>    <tfd:DigitalTaxStamp xmlns:tfd=/http://www.sat.gob.mx/TimbreFiscalDigital/ TimbradoDate=/2023-08-15T10:35:22/ SATCertificateNumber=/SAT_CERTIFICATE/ CertifyingEntityRfc=/SAT_RFC/ CFDigitalSeal=/FICTITIOUS_DIGITAL_CFD_SEAL/ SATSeal=/FICTITIOUS_DIGITAL_SAT_SEAL/ UUID=/9b824153-a667-4a46-829d/ Version=/1.1/ xsi:schemaLocation=/http://www.sat.gob.mx/TimbreFiscalDigital http://www.sat.gob.mx/sitio_internet/cfd/TimbreFiscalDigital/TimbreFiscalDigitalv11.xsd/ />  </cfdi:Complement></cfdi:Comprobante>
I would like to know how to:
1. Access specific nodes within this XML structure.
2. Retrieve the values of their attributes using AL in Business Central.
3. Create a loop to handle cases where the XML contains multiple /Concept/ nodes, extracting attribute values from each of them.
  • Suggested answer
    xMetAK Profile Picture
    2 on at
    How to Access XML Nodes and Retrieve Attribute Values After Loading XML into InStream Variable?
    Hi!
     
    I dont understand why this question was not answered before...
    It's not a mistery not difficult to understand... unless you are new in AL or you just response to answer randomly (wich i think is what mvp's do nowadays...)
     
    Fast response:
    XMLElement has attributes. XLNode no.
     
    So u need to get the xmlElement of the xmlNode and then the attributes:
     
    var
            xmlDoc: XmlDocument;
            XmlNodeList: XmlNodeList;
            XmlNode: XmlNode;
            xmlAttribute: XmlAttribute;
            xmlAttributes: XmlAttributeCollection;
     
            UUID: Text[50];
            FileFilterTxt: Label 'XML Files(*.xml)|*.xml|All Files(*.*)|*.*', Locked = true;
            FileInStream: InStream;
        begin
            UploadIntoStream(FileFilterTxt, FileInStream);
            XmlDocument.ReadFrom(FileInStream, XmlDoc);
            XmlDoc.SelectNodes('//cfdi:Complemento/tfd:TimbreFiscalDigital', xmlnodelist);
            if xmlnodelist.Count <> 0 then begin
                xmlnodelist.Get(0, XmlNode);
                xmlAttributes := XmlNode.AsXmlElement().Attributes(); //HERE
                foreach xmlAttribute in xmlAttributes do
                    IF xmlAttribute.Name = 'UUID' then
                        UUID := COPYSTR(xmlAttribute.Value, 1, 50);
            end;
     
    There is a reason why is like this wich lead us to teory, but to be fast in my answer xmlNode is just a type of object, and xmlElement is part of the DOM structure (so it has properties like attributes) while XMlNode could be an attribute itself or 20 other things
     
    Best regards!
  • isabtogumon Profile Picture
    99 on at
    How to Access XML Nodes and Retrieve Attribute Values After Loading XML into InStream Variable?
    Hello YUN ZHU, thanks for replaying
     
    In the first example you mentioned seems to be deprecated in the latest version of BC, and unfortunately, the second example doesn't demonstrate how to retrieve attribute values, which is what I'm trying to accomplish.
  • isabtogumon Profile Picture
    99 on at
    How to Access XML Nodes and Retrieve Attribute Values After Loading XML into InStream Variable?
    Hello Inge M. Bruvikthanks for replying
     
    I've reviewed the method you shared for retrieving element attributes, however, it's not entirely clear to me. Could you provide a practical example to see how it works?
     
    It's worth mentioning that I've made adjustments to my code, with which I can now access the 'Concept' node, but I haven't been able to access the attributes.
     
     
    Regards.
  • Suggested answer
    YUN ZHU Profile Picture
    81,711 Super User 2025 Season 1 on at
    How to Access XML Nodes and Retrieve Attribute Values After Loading XML into InStream Variable?
  • Suggested answer
    Inge M. Bruvik Profile Picture
    1,021 Moderator on at
    How to Access XML Nodes and Retrieve Attribute Values After Loading XML into InStream Variable?
    For selecting a single node you can use the select single node method:
    For selecting an attribute use the 
     
     
    For looping through node you can select nodes into a nodes list and loop through them in an for each loop
     
    I hope this helps you further.
     
    And you can circle back with more detailed questions it can be a bit tricky to format the xpath queries correctly if you are not familiar with them,
     
    And easier approach can also be to use the xmlbuffer instead of handling it as an xml document. But handling it as an xmldocument is the most efficient way if you are working on large xml files that you only want specific information from.

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,217 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,978 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans