如何使用 C# 从 Azure Blob 存储中检索 xml 文件

how to retrieve an xml file from azure blob storage using c#

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

 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;

            }

尝试使用此代码,我在我的系统中测试能够将 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 文档本地化