Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Suggested answer

XML file not uploading to blob

Posted on by 303
Hi All,
 
I am trying to upload my xml to blob storage. It is getting uploaded but the file size is 3B. I am suspecting the stream has null value sue to which the xml is not appearing.
Please find the below code I have written.
  streamIO.write(xmlDoc);
 
  XX_VendPaymIntegrationHelper::uploadToBlob(parameters.BlobStorageConnectionString,
                                              parameters.BlobStorageContainer,
                                             parameters.BlobStorageOutboundFolder,
                                             fileName,
                                             streamIO,xmlDoc);
 
  public static void uploadToBlob(HS_VendPaymIntBlobStorageConnectionString _connectionString,
                                     HS_VendPaymIntBlobStorageContainer _container,
                                     HS_VendPaymBlobStorageOutboundFolder _folder,
                                     Filename _filename,
                                     CommaTextStreamIo _streamIO, XMLDocument _xmlDoc )
  {
      try
      {
          CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount::Parse(_connectionString);
          CloudBlobClient     blobClient = storageAccount.CreateCloudBlobClient();
          CloudBlobContainer  blobContainer = blobClient.GetContainerReference(_container);
          //blobContainer.CreateIfNotExists(null, null); //create blob container

          Filename            fullName = _folder ? _folder + "/" + _filename : _filename;
          CloudBlockBlob      destBlob = blobContainer.GetBlockBlobReference(fullName);
          System.IO.MemoryStream memstream = _streamIO.getstream();
          destBlob.UploadFromStream(memstream, null, null, null);

          
      
      }
      catch
      {
          throw error("Blob error");
      }
  }
 
 
Please help.
 
Thanks,
Priya
  • Martin Dráb Profile Picture
    Martin Dráb 230,149 Most Valuable Professional on at
    XML file not uploading to blob
    As you see, if you look just at the final result, you have no idea where your code failed. You need to debug it to get a better idea where things went wrong. I can't debug it for you. For example, it's possible that the code you showed us is perfect but you have nothing in _xmlDoc. I don't know; you didn't tell us anything about testing it.
     
    If I was you, I would test whether the data is written to the stream. If not, there is no point trying to write it to a blob and checking the size, because you already know you have no data to write. But if it's there, you'll know that your problem is in writing to the blob, not to the stream. This is an example of how you can isolate the problem.
     
    By the way, it would be better (both for you and the people you share your code with) if you created a simple, standalone example that can be executed.
  • PriyaDutta Profile Picture
    PriyaDutta 303 on at
    XML file not uploading to blob
    Hi Martin,
     
    I tried the below example you suggested. Now the blob file shows 0B . 
    Please see the below code for reference.
     
     public static void uploadToBlob(HS_VendPaymIntBlobStorageConnectionString _connectionString,
                                        HS_VendPaymIntBlobStorageContainer _container,
                                        HS_VendPaymBlobStorageOutboundFolder _folder,
                                        Filename _filename,, xmldocument _xmlDoc )
     {
         try
         {
             CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount::Parse(_connectionString);
             CloudBlobClient     blobClient = storageAccount.CreateCloudBlobClient();
             CloudBlobContainer  blobContainer = blobClient.GetContainerReference(_container);
             //blobContainer.CreateIfNotExists(null, null); //create blob container
    
             Filename            fullName = _folder ? _folder + "/" + _filename : _filename;
             CloudBlockBlob      destBlob = blobContainer.GetBlockBlobReference(fullName);
             System.IO.MemoryStream stream = new System.IO.MemoryStream();
    
             XmlWriter xmlWriter = XmlWriter::newStream(stream);
    
             _xmlDoc.writeTo(xmlWriter);
             xmlWriter.flush();
             destBlob.UploadFromStream(stream, null, null, null);
    
             
         
         }
         catch
         {
             throw error("Blob error");
         }
     }
     
     
    Please help.
     
    Thanks,
    Priya
  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,149 Most Valuable Professional on at
    XML file not uploading to blob
    You may mean either the X++ class XMLDocument or .NET class System.Xml.XmlDocument. In either case, writing the object reference to a stream is wrong.
     
    Use writeTo() method of the document object instead.
     
    For example:
    System.IO.MemoryStream stream = new System.IO.MemoryStream();
    
    XmlWriter xmlWriter = XmlWriter::newStream(stream);
    
    xmlDoc.writeTo(xmlWriter);
    writer.flush();
  • PriyaDutta Profile Picture
    PriyaDutta 303 on at
    XML file not uploading to blob
    Hi Martin,
     
    I debugged the code and Length is= 3
    Xmldoc is of XMLDocument datatype.
     
  • Martin Dráb Profile Picture
    Martin Dráb 230,149 Most Valuable Professional on at
    XML file not uploading to blob
    You should do more work on the the problem isolation, instead of just trying random pieces of code. For instance, you said you might have no data written to _streamIO. If it's the case, resetting the position won't help you - it won't change the fact that you haven't written anything to the stream. But maybe your assumption is wrong and you actually have data there. You should test the assumption, e.g. by looking at the Length property of the stream.
     
    Regarding your question about streamIO.write(xmlDoc), what is the data type of your xmlDoc variable?
  • PriyaDutta Profile Picture
    PriyaDutta 303 on at
    XML file not uploading to blob
    Hi Martin, 
    I have tried adding position=0 but same issue.
     try
     {
         CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount::Parse(_connectionString);
         CloudBlobClient     blobClient = storageAccount.CreateCloudBlobClient();
         CloudBlobContainer  blobContainer = blobClient.GetContainerReference(_container);
         //blobContainer.CreateIfNotExists(null, null); //create blob container
    
         Filename            fullName = _folder ? _folder + "/" + _filename : _filename;
         CloudBlockBlob      destBlob = blobContainer.GetBlockBlobReference(fullName);
         System.IO.MemoryStream memstream = _streamIO.getstream();
         memstream.Position = 0;
         destBlob.UploadFromStream(memstream, null, null, null);
    
         
     
     }
    The question is it this the correct way to write to stream ?  Is there any alternative to convert the xml file to stream ?
    streamIO.write(xmlDoc);
    And I am using D365FO.
  • Martin Dráb Profile Picture
    Martin Dráb 230,149 Most Valuable Professional on at
    XML file not uploading to blob
    Well, if you suspect that _streamIO is empty, test it. We can't do it for you; you're the only one who can run your complete code.
     
    By the way, it's possible that you've written data successfully to the stream but it's object is pointing to the end of the stream, therefore you need to reset the position to the beginning of the stream. It may be as simple as assigning 0 to the Position property (depending on the type of the stream).
     
    By the way, which version of AX is it about?

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans