【问题标题】:Merge Multiple XML files in to one XML file in the azure storage using azure functions使用 azure 函数将多个 XML 文件合并到 azure 存储中的一个 XML 文件
【发布时间】:2022-11-04 19:53:08
【问题描述】:

我正在学习 azure 函数作为其中的一部分,我想将相同 xml 结构的两个不同 xml 文件合并到一个 xml 文件中。有人可以帮助我如何使用 azure 函数来做到这一点吗?

通过异步方法合并 xml 时,我无法使用 XmlDocument.Open 打开 xmldocument,因为没有为 XmlDocument 打开定义。我们如何打开 xmldocument 数据?

这是我的代码,我在通过 memorystream 打开 xmldocument 时卡住了

        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);
                    

                }
            }
        }

【问题讨论】:

  • 请说明您遇到了什么错误以及您在哪条线路上遇到了问题。
  • 嗨,我在 var inputDocument = XmlDocument.Open(memoryStream, XmlDocument.Import); 行收到错误错误说 theta XmlDocument 不包含 Open 的定义我正在使用与 pdf 合并类似的定义,所以我也尝试将其用于 xml 合并,但出现上述错误。所以请帮助我如何前进从这里或者是否有任何替代我可以工作的地方....谢谢
  • 您可以使用您在评论中提供的信息更新问题吗?因为你的问题不符合社区标准。除非它被更新,否则它将被关闭。

标签: c# azure azure-functions concatenation azure-storage


【解决方案1】:

我建议您阅读 blob 文件并将 blob 内容添加到单个单独的文件中。如果要合并excel文件,我们有两种方法来实现。要么你可以使用Microsoft.Office.Interop.Excel打包和使用File Stream.

在这里,我从 blob 收集文件并将文件的内容添加到单独的文件中。

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("	" + blobItem.Name);
    BlobClient blobClient = containerClient.GetBlobClient(blobItem.Name);
    string downloadFilePath = localFilePath.Replace(".xlsx", blobItem.Name.Replace(".xlsx","")+"<Filename.xlsx>");
    Console.WriteLine("
Downloading blob to
	{0}
", downloadFilePath);
    await blobClient.DownloadToAsync(downloadFilePath);
}

使用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;

参考C# Corner articleusing stream向excel文件添加内容。

【讨论】:

    猜你喜欢
    • 2021-04-23
    • 2019-12-25
    • 1970-01-01
    • 1970-01-01
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-24
    相关资源
    最近更新 更多