0

I'm sure there's something blindingly obvious I'm missing here.

I have an ASP.NET MVC2 app with an XML doc at /content/mydoc.xml. I'm trying to load it using XmlTextReader:

XmlTextReader reader = new XmlTextReader("/content/mydoc.xml");

Stepping through, I can see that this is being resolved to file:///C:/content/mydoc.xml

I know I can use Server.MapPath() to get the file path but that seems rather hackish given that the XML doc is available via http.

Is there a way to get XmlTextReader to properly resolve the URL?

Ben Hughes
  • 1,539
  • 13
  • 20

1 Answers1

1

How about

XmlTextReader reader = 
    new XmlTextReader(Url.GenerateContentUrl("~/content/mydoc.xml"));

Of course, you'll need an UrlHelper instance to hand to accomplish this (available as the Url field in view and controller).

EDIT

If I know where the file lives I'd prefer to go for it as a file, not with the overhead of HTTP. As such MapPath seems like a good choice here.

spender
  • 117,338
  • 33
  • 229
  • 351
  • Thanks. However, this just seems to resolve ~/content/mydoc.xml back to /content/mydoc.xml. I can use the answer from http://stackoverflow.com/questions/1288046/how-can-i-get-my-webapps-base-url-in-asp-net-mvc to get the base URL and construct the full URL, but that seems rather convoluted for something that seems like it should be simple – Ben Hughes Nov 03 '10 at 01:58
  • +1 for accessing the file via the file system rather than HTTP, unless you have a specific reason to do it. – jasper Nov 03 '10 at 02:19
  • Cheers guys - it does indeed make more sense. – Ben Hughes Nov 03 '10 at 22:03