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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. However, this just seems to resolve ~/content/mydoc.xml back to /content/mydoc.xml. I can use the answer from stackoverflow.com/questions/1288046/… to get the base URL and construct the full URL, but that seems rather convoluted for something that seems like it should be simple
+1 for accessing the file via the file system rather than HTTP, unless you have a specific reason to do it.
Cheers guys - it does indeed make more sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.