skip to Main Content

I am learning azure functions as a part of it, i want to merge two different xml files of same xml structures in to a single xml file. Can some one please help me how to do that using azure functions ?

While merging the xml through async method, i am unable to open the xmldocument using XmlDocument.Open as there is no definition Open for XmlDocument. How do we open the xmldocument data ?

This is my code and i got stuck at while open the xmldocument through memorystream

        private async Task<IList> MergeFileAsync(CloudBlobContainer container, string[] blobFiles)
        {
            XmlDocument outputDocument = new XmlDocument();

            foreach(String fileblob in blobFiles)
            {
                string file = $"" + blobFiles;

                CloudBlockBlob blockBlob = container.GetBlockBlobReference(file);
                using(var memoryStream = new MemoryStream())
                {
                    await blockBlob.DownloadToStreamAsync(memoryStream);

                    string contents = blockBlob.DownloadTextAsync().Result;

                      //stuck here
                    var inputDocument = XmlDocument.Open(memoryStream, XmlDocument.Import);
                    

                }
            }
        }

2

Answers


  1. I would suggest you read the blob files and add the blob content to the single separate file. If you want to combine the excel file, we have two approaches to achieve. Either you can use Microsoft.Office.Interop.Excel package and using File Stream.

    Here I have collected the file from blob and adding content of the file in separate file.

    string localPath = "<File path>";
    string fileName = "<FiletoCombine>";
    string localFilePath = Path.Combine(localPath, fileName);
    BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
    string containerName = "<Your container Name>";
    
    BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
    
    var val = containerClient.GetBlobs();
    foreach (BlobItem blobItem in val)
    {
        Console.WriteLine("t" + blobItem.Name);
        BlobClient blobClient = containerClient.GetBlobClient(blobItem.Name);
        string downloadFilePath = localFilePath.Replace(".xlsx", blobItem.Name.Replace(".xlsx","")+"<Filename.xlsx>");
        Console.WriteLine("nDownloading blob tont{0}n", downloadFilePath);
        await blobClient.DownloadToAsync(downloadFilePath);
    }
    

    Using Microsoft.Office.Interop.Excel :

    Excel.Application xlApp;
    Excel.Workbook xlWorkBook;
    Excel.Worksheet xlWorkSheet;
    xlApp = new Excel.Application();
    xlWorkBook = xlApp.Workbooks.Open(fileName);
    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
    
    for (int i = 0; i < dataGridView3.Rows.Count - 1; i++)
    {
        for (int j = 0; j < dataGridView3.Columns.Count; j++)
        {
            xlWorkSheet.Cells[i + 4, j + 1] = dataGridView3.Rows[i].Cells[j].Value.ToString();
        }
    }
    
    xlWorkBook.SaveAs(downloadFilePath);
    object misValue = System.Reflection.Missing.Value;
    xlWorkBook.Close(true, misValue, misValue);
    xlApp.Quit();
    xlWorkSheet = null;
    xlWorkBook = null;
    xlApp = null;
    

    Refer to C# Corner article for using stream to add content to excel file.

    Login or Signup to reply.
  2. As you have already got xmldocument content, you have to instantiate xmldocument with content.

    string contents = blockBlob.DownloadTextAsync().Result;
    outputDocument.LoadXml(contents);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search