1

In order to prevent the usual issues where I have an Xml file in a folder in one project and want to access it from other projects and have to deal with the file path issues, I want to download the Xml file contents directly from Azure blob storage where it resides now.

Not sure how to accomplish that although I see many examples of how to download images into streams, not sure how that works for Xml.

I am currently using the following ( which works until I move the Xml file)

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

I want to eliminate all the bs associated with path combining and just download the xml from the file on Azure, i.e. replacing the string

@"ServicesDataFiles\MRNavigationMenu.xml"

with

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

Naturally, that wouldn't work but there must be someway to load that xml into a file variable for use with the method.

Note: That is a publicly accessible file on azure for testing.

dinotom
  • 4,990
  • 16
  • 71
  • 139
  • Can you describe what `NavigationMenusFromXml` class does in your code? – Gaurav Mantri Feb 08 '17 at 17:41
  • 1
    This is a good scenario, I do the same for configuration across various projects. It doesn't sound like there's anything Azure about this. Since it's a public blob, it's just a normal HTTP call. See http://stackoverflow.com/questions/27108264/c-sharp-how-to-properly-make-a-http-web-get-request – David Betz Feb 08 '17 at 17:43
  • @Guarav...it loads the website menu headers from the xml file. That works fine , the problem is the file isnt downloading because the path is generating wrong. i want to eliminate that by downloading directly from Azure – dinotom Feb 08 '17 at 17:49
  • @David Betz...I figured that's the route i would have to go, just thought maybe there was an easier method. Not that doing the above is that hard..lol – dinotom Feb 08 '17 at 18:02

1 Answers1

0

Use a memory stream. Remember to set position to zero before attempting to read.

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


        }
    }



}
jdweng
  • 33,250
  • 2
  • 15
  • 20