从Azure Blob存储中下载xml文件的内容

时间:2017-02-08 17:38:40

标签: c# xml azure

为了防止我在一个项目的文件夹中有Xml文件并希望从其他项目访问它并且必须处理文件路径问题的常见问题,我想直接从下载Xml文件内容现在它所在的Azure blob存储。

虽然我看到很多关于如何将图像下载到流中的示例,但我不知道如何实现这一点,但不确定它对Xml有何用处。

我目前正在使用以下内容(在移动Xml文件之前一直有效)

public class MenuLoader
{
    //var rootpath = HttpContext.Current.Server.MapPath("~");
    private static readonly string NavMenuXmlPath = Path.Combine(ServicesHelpers.GetClassLibraryRootPath(),
        @"ServicesDataFiles\MRNavigationMenu.xml");
    );

    //load the menus, based on the users role into the AppCache
    public static void LoadMenus(IPrincipal principal)
    {
        var navXml = new NavigationMenusFromXml(NavMenuXmlPath);
        var nmim = new NavigationMenuItemManager(navXml);
        AppCache.Menus = nmim.Load(principal);
    }
}

我想消除与路径组合相关的所有bs,只需从Azure上的文件下载xml,即替换字符串

@ “ServicesDataFiles \ MRNavigationMenu.xml”

https://batlgroupimages.blob.core.windows.net:443/files/MRNavigationMenu.xml

当然,这不起作用,但必须将xml加载到文件变量中以便与方法一起使用。

注意:这是azure上可公开访问的文件,用于测试。

1 个答案:

答案 0 :(得分:-1)

使用内存流。在尝试阅读之前,请记住将位置设置为零。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Xml;


namespace ConsoleApplication1
{
    class Program
    {
        const string URL = "https://batlgroupimages.blob.core.windows.net/files/MRNavigationMenu.xml";
        static void Main(string[] args)
        {
            MemoryStream stream = new MemoryStream();
            XmlWriter writer = XmlWriter.Create(stream);
            XmlDocument doc = new XmlDocument();
            doc.Load(URL);
            writer.WriteRaw(doc.OuterXml.ToString());
            writer.Flush();


        }
    }



}
相关问题