Ben Hughes
Ben Hughes

Reputation: 1539

Using XmlTextReader to read a file on the same server via http

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?

Upvotes: 0

Views: 1190

Answers (1)

spender
spender

Reputation: 120508

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.

Upvotes: 1

Related Questions