【问题标题】:how to retrieve an xml file from azure blob storage using c#如何使用 c# 从 azure blob 存储中检索 xml 文件
【发布时间】:2021-12-23 17:09:38
【问题描述】:

我想从 Blob 存储中检索 2 个 Xml 文件并使用 c# 返回这些文件。不用担心连接、容器名和文件名。 我尝试使用下面的代码从 blob 中获取 xml 作为字符串并作为列表返回,但我需要一个作为 Xmlfile 的返回。

 public List<string> GetXmlFiles(List<string> Xmlname)
        {
            string storageConnectionString = "";
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString);
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference("ContainerName");
            CloudBlockBlob blob = container.GetBlockBlobReference("fileName");
            string xml =blob.DownloadTextAsync().ToString();
            List<string> XmlFile = new List<string>(xml.Split(' '));
        
            return XmlFile;

            }

【问题讨论】:

标签: c# .net azure azure-blob-storage


【解决方案1】:

尝试使用此代码,我在我的系统中进行了测试,能够将 xml 文件下载为文件

using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Blob;
using System;
using System.IO;

namespace downloadxml
{
    class Program
    {
        static void Main(string[] args)
        {
            
            string storageConnectionString = "Connection string”
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString);
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference("test");
            var allblobs = container.ListBlobs(useFlatBlobListing: true);
            List<String> files = new List<String>();
            foreach (var blob in allblobs)
            {
                string name = ((CloudBlockBlob)blob).Name;
                CloudBlockBlob blockBlob = container.GetBlockBlobReference(name);
                string path = (@"localk path”)
                string[] names = name.Split("/");
                string newPath = "";
                string fileName = "";
                for (int i = 0; i < names.Length; i++)
                {
                    if (i != (names.Length - 1))
                    {
                        path = path + names[i] + "\\";
                        newPath = newPath + "\\" + names[i];
                    }
                    fileName = names[(names.Length - 1)];
                }
                string filePath = path + fileName;
                if (Directory.Exists(path))
                {
                    blockBlob.DownloadToFile(filePath, FileMode.OpenOrCreate);
                }
             files.Add(filePath);
            }
        }
    }
}

输出

我有两个xml文件xmlfile1xmlfile2

将两个文件作为xmlfile1xmlfile2 下载为本地xml 文档

【讨论】:

  • 我不想下载它。我想在函数中将这 2 个 Xml 作为文件返回
  • 两个 xml 作为单个文件?
  • 没有 2 个 xml 作为单独的文件
  • 根据您的要求,您需要返回 2 个文件,不保存文件就无法返回文件,所以我编辑了下载文件并将这两个文件添加到字符串列表的答案,所以您可以返回函数中的两个文件路径
  • 如果答案对您有帮助,您可以接受它作为答案(单击答案旁边的复选标记,将其从灰色切换为已填充。)。这对其他社区成员可能是有益的。谢谢
猜你喜欢
  • 1970-01-01
  • 2021-01-31
  • 2016-08-13
  • 1970-01-01
  • 2015-06-20
  • 1970-01-01
  • 1970-01-01
  • 2018-04-25
  • 1970-01-01
相关资源
最近更新 更多