繁体   English   中英

如何使用 c# 从 azure blob 存储中检索 xml 文件

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

我想从 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;

            }

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

在此处输入图片说明

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM