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 :
Finance | Project Operations, Human Resources, ...
Answered

Xml file generating blank while uploading to azure blob storage

(0) ShareShare
ReportReport
Posted on by

Hello Everyone,

I have written a code to create a XML file and that file i am uploading to the Azure blob storage.

The file is successfully uploaded to the blob storage but the file is generating blank.

if i save the file in @"C:\\" C drive file is generating. but that's not my requirement. 

I have written the following code to create XML file.

Please help me with it.

 #Localcodepage       
        XmlNode                         IT_TRANSACTION, RETAILLINEITEM, TAX, DISCOUNT, itemTENDER, itemLine, itemStoreId, itemTAX, itemDiscount;
        XmlNode                         newNode;
        XmlAttribute                    newNodeAttribute;
        XmlAttribute                    secondNodeAttribute;
        XmlDocument                     doc;
        XmlElement                      nodeXmlRetailLineItem, nodeXmlTaxLineItems, nodeXmlDiscountItems, nodeXmlTender, nodeXmlTax;
        XmlElement                      nodeIT_Transactions, nodeLineData, nodeTaxData, nodeDiscount, nodeTenderData;
        PwcIntegSAPSalesExportStaging   sapSalesExportStaging ,sapSalesExportStagingTrans, stagingLoc;
        RetailTransactionPaymentTrans   transactionPaymentTrans;
        AsciiStreamIo                   file;
        str                             fileContent;
        const str           filename = 'RetailSales.xml';
        CommaStreamIo             io = CommaStreamIo::constructForWrite();
        
        
       
        doc = XmlDocument::newBlank();
                
        newNode = doc.createElement("ABC");
        newNodeAttribute = doc.CreateAttribute("xmlns:");
        newNodeAttribute.Value("XYZ");
        newNode.attributes().setNamedItem(newNodeAttribute);
        doc.appendChild(newNode);

        // IT_TRANSACTION tag initiated
        IT_TRANSACTION      = doc.createElement("IT_TRANSACTION");
        newNode.appendChild(IT_TRANSACTION);
        
        while select sapSalesExportStaging group by sapSalesExportStaging.RETAILSTOREID
            where sapSalesExportStaging.FileCreated == NoYes::No
        {
            select * from stagingLoc
                    where stagingLoc.RETAILSTOREID == sapSalesExportStaging.RETAILSTOREID;
            // item tag initiated
            itemStoreId     = doc.createElement("item");
            IT_TRANSACTION.appendChild(itemStoreId);

            // Fetching retail strore header data
            itemStoreId.appendChild(doc.createElement("RETAILSTOREID")).appendChild(doc.createTextNode(stagingLoc.RETAILSTOREID));
            itemStoreId.appendChild(doc.createElement("BUSINESSDAYDATE")).appendChild(doc.createTextNode(stagingLoc.BUSINESSDAYDATE));
            itemStoreId.appendChild(doc.createElement("TRANSTYPECODE")).appendChild(doc.createTextNode(stagingLoc.TRANSTYPECODE));
            itemStoreId.appendChild(doc.createElement("WORKSTATIONID")).appendChild(doc.createTextNode(stagingLoc.WORKSTATIONID));
            itemStoreId.appendChild(doc.createElement("TRANSNUMBER")).appendChild(doc.createTextNode(stagingLoc.TRANSNUMBER));
            itemStoreId.appendChild(doc.createElement("BEGINTIMESTAMP")).appendChild(doc.createTextNode(stagingLoc.BEGINTIMESTAMP));
            itemStoreId.appendChild(doc.createElement("ENDTIMESTAMP")).appendChild(doc.createTextNode(stagingLoc.ENDTIMESTAMP));
            itemStoreId.appendChild(doc.createElement("DEPARTMENT")).appendChild(doc.createTextNode(stagingLoc.DEPARTMENT));
            itemStoreId.appendChild(doc.createElement("OPERATORID")).appendChild(doc.createTextNode(stagingLoc.OPERATORID));
            itemStoreId.appendChild(doc.createElement("TRANSCURRENCY")).appendChild(doc.createTextNode(stagingLoc.TRANSCURRENCY));
             
            // Retail line item node initiated
            nodeXmlRetailLineItem = itemStoreId.appendChild(doc.createElement("RETAILLINEITEM"));

            // Tender node initiated
            nodeXmlTender = itemStoreId.appendChild(doc.createElement("TENDER"));

            while select sapSalesExportStagingTrans
                    where sapSalesExportStagingTrans.RETAILSTOREID == sapSalesExportStaging.RETAILSTOREID
            {
                // Tag item initiated for line data
                itemLine    = doc.createElement("item");
                nodeXmlRetailLineItem.appendChild(itemLine);

                // Fetching line data
                itemLine.appendChild(doc.createElement("RETAILNUMBER")).appendChild(doc.createTextNode(sapSalesExportStagingTrans.Line_RetailNumber));
                itemLine.appendChild(doc.createElement("RETAILTYPECODE")).appendChild(doc.createTextNode(sapSalesExportStagingTrans.Line_RetailTypeCode));
                itemLine.appendChild(doc.createElement("ITEMIDQUALIFIER")).appendChild(doc.createTextNode(sapSalesExportStagingTrans.Line_ItemIdQualifier));
                itemLine.appendChild(doc.createElement("ITEMID")).appendChild(doc.createTextNode(sapSalesExportStagingTrans.Line_ItemId));
                itemLine.appendChild(doc.createElement("RETAILQUANTITY")).appendChild(doc.createTextNode(sapSalesExportStagingTrans.Line_RetailQuantity));
                itemLine.appendChild(doc.createElement("SALESUOM")).appendChild(doc.createTextNode(sapSalesExportStagingTrans.Line_SalesUOM));
                itemLine.appendChild(doc.createElement("SALESAMOUNT")).appendChild(doc.createTextNode(sapSalesExportStagingTrans.Line_SalesAmount));
                itemLine.appendChild(doc.createElement("NORMALSALESAMT")).appendChild(doc.createTextNode(sapSalesExportStagingTrans.Line_NormalSalesAmt));

                // Tax node initiated
                nodeXmlTax = itemLine.appendChild(doc.createElement("TAX"));
                // Tag item initiated for tax data
                itemTax         = doc.createElement("item");
                nodeXmlTax.appendChild(itemTax);

                // Fetching tax data
                itemTAX.appendChild(doc.createElement("TAXNUMBER")).appendChild(doc.createTextNode(sapSalesExportStagingTrans.Tax_Taxnumber));
                itemTAX.appendChild(doc.createElement("TAXTYPECODE")).appendChild(doc.createTextNode(sapSalesExportStagingTrans.Tax_TaxTypeCode));
                itemTAX.appendChild(doc.createElement("TAXAMOUNT")).appendChild(doc.createTextNode(sapSalesExportStagingTrans.Tax_TaxAmount));

                // discount node initiated
                nodeXmlDiscountItems = itemLine.appendChild(doc.createElement("DISCOUNT"));
                // Tag item initiated for discount data
                itemDiscount    = doc.createElement("item");
                nodeXmlDiscountItems.appendChild(itemDiscount);

                // Fetching discount data
                itemDiscount.appendChild(doc.createElement("DISCNUMBER")).appendChild(doc.createTextNode(sapSalesExportStagingTrans.Discount_DiscNumber));
                itemDiscount.appendChild(doc.createElement("DISCTYPECODE")).appendChild(doc.createTextNode(sapSalesExportStagingTrans.Discount_DiscTypeCode));
                itemDiscount.appendChild(doc.createElement("REDUCTIONAMOUNT")).appendChild(doc.createTextNode(sapSalesExportStagingTrans.Discount_ReductionAmount));
               
                select firstonly transactionPaymentTrans
                        where transactionPaymentTrans.Store    == stagingLoc.RETAILSTOREID
                            && transactionPaymentTrans.lineNum == any2Int(sapSalesExportStagingTrans.Line_RetailNumber);

                // Tag item initiated for tender
                itemTENDER    = doc.createElement("item");
                nodeXmlTender.appendChild(itemTENDER);
                    
                // Fetching tender data
                itemTENDER.appendChild(doc.createElement("TENDERNUMBER")).appendChild(doc.createTextNode(transactionPaymentTrans.receiptId));
                itemTENDER.appendChild(doc.createElement("TENDERTYPECODE")).appendChild(doc.createTextNode(transactionPaymentTrans.tenderType));
                itemTENDER.appendChild(doc.createElement("TENDERAMOUNT")).appendChild(doc.createTextNode(any2Str(transactionPaymentTrans.amountTendered)));
                itemTENDER.appendChild(doc.createElement("TENDERCURRENCY")).appendChild(doc.createTextNode(transactionPaymentTrans.currency));
                   
            }
        }       
        XMLWriter writer = XMLWriter::newFile(filename);
        //doc.writeContentTo(writer);
        doc.writeTo(writer);

        System.IO.Stream stream = io.getStream();
        stream.Position = 0;
        System.IO.StreamReader sReader = new System.IO.StreamReader(stream);
        fileContent = sReader.ReadToEnd();
                
        this.uploadFileToAzure(stream, filename);
    }

I have the same question (0)
  • Suggested answer
    ergun sahin Profile Picture
    8,826 Moderator on at

    Probably after  reader read, stream lost position.

    You can try UploadFromStream to

  • Community Member Profile Picture
    on at

    Can you please explain me.

  • Suggested answer
    Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at

    It seems that 90% of your code isn't related to the problem, because it's about details of the content. Let's throw away unrelated code, so we can focus on the actual problem.

    const str filename = 'RetailSales.xml';
    CommaStreamIo io = CommaStreamIo::constructForWrite();
    
    XmlDocument doc = XmlDocument::newBlank();
    XmlNode newNode = doc.createElement("ABC");
    doc.appendChild(newNode);
    
    XMLWriter writer = XMLWriter::newFile(filename);
    doc.writeTo(writer);
    
    System.IO.Stream stream = io.getStream();
    stream.Position = 0;
    System.IO.StreamReader sReader = new System.IO.StreamReader(stream);
    str fileContent = sReader.ReadToEnd();
    		
    this.uploadFileToAzure(stream, filename);

    There you create XML document and write it to XMLWriter, but then you don't use it for anything. Therefore your effective code is this:

    CommaStreamIo io = CommaStreamIo::constructForWrite();
    
    System.IO.Stream stream = io.getStream();
    stream.Position = 0;
    System.IO.StreamReader sReader = new System.IO.StreamReader(stream);
    str fileContent = sReader.ReadToEnd();
    		
    this.uploadFileToAzure(stream, 'RetailSales.xml');

    Now it's obvious what's going on. You never write any data to the stream that you're uploading, therefore you'll indeed get an empty file. If your goal is creating an XML file, forget CommaStreamIo completely, because that's for comma-separated fikes, not XML.

  • ergun sahin Profile Picture
    8,826 Moderator on at

    When I saw the FileContent variable, I thought,, you tested it in debug and it was full. As Martin said, you've never filled the stream.

    stackoverflow.com/.../how-to-save-xmldocument-to-a-stream

  • Community Member Profile Picture
    on at

    OKay, Then what should i write instead of CommaStreamIO?

  • Verified answer
    Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at

    You've put your data to XMLDocument and you need a Stream object for uploadFileToAzure(). Therefore the only thing that you need to do is writing XML from XMLDocument to a MemoryStream. Forget files, CommaStreamIO and all such things - they're not relevant to your actual requirement.

    Check out this example:

    XmlDocument doc = XmlDocument::newBlank();
    XmlNode newNode = doc.createElement("ABC");
    doc.appendChild(newNode);
    
    using (var stream = new System.IO.MemoryStream())
    {
        XMLWriter writer = XMLWriter::newStream(stream);
        doc.writeTo(writer);
        writer.flush();
    
        File::SendFileToUser(stream, 'test.xml');
    }

    SendFileToUser() is there for testing purposes. When you successfully test that you're getting data in the stream, you can add writing to the blob storage. Implementing and testing things one by one makes finding bugs much easier.

  • Community Member Profile Picture
    on at

    I am getting the error in stream while debugging. Is this error impacting?

    Please help me to resolve this.stream-error.jpg

  • Verified answer
    Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at

    No, I don't think that you should worry about whether the debugger can show you values of ReadTimeout and WriteTimeout properties. Do you need to know their values? If not, just ignore them. Focus on your goal instead.

  • Community Member Profile Picture
    on at

    Thank you. Its working.

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 > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 611 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 529 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 285 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans