5

I am having this issue on my mind for 3 days now.
I have an xml file that is marked as Content and Always Copy.
The file was copied to:
C:\Users\avi\Documents\Visual Studio 2010\Projects\ExpressBroker\ExpressBroker\bin\XMLMetadata\Actions.1.xml

When accessing to the file:

//like that:
XDocument actions = XDocument.Load("bin\\XMLMetadata\\Actions.1.xml");
//or like that:
XDocument actions = XDocument.Load("XMLMetadata\\Actions.1.xml");
//or like that:
XDocument actions = XDocument.Load("Actions.1.xml");

I get the following exception: Exception Details: System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Program Files\IIS Express\bin\XMLMetadata\Actions.1.xml'.

Why is it been searched in the IIS folder? how do i access the file?

I am using IIs Express with VWD2010

1
  • please oh please never use a syntax like the below there is no need for it.use ~ 100% not to mention why would you check in bin for a static resource? Commented Oct 26, 2011 at 16:55

3 Answers 3

6

You need to have web application's relative path by using

Server.MapPath("/")+"bin\\XMLMetadata\\Actions.1.xml" 

like that.

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

5 Comments

what is the namespace of server? i don't have it available. thanks
@SexyMF - Why do you need a "namespace of server"?
It should be available on your web application on any page. if you are outside the page then get it from System.Web.HttpContext.Current.Server assuming that you are running this code on web application so the httpcontext's "Current" will not be null.
why would you not just use whats meant to be used here and thats the '~' character.
ya ~ too. Also before loading into XDocument, it'd be good to check if file exists by if(System.IO.File.Exists(<filepath>)){ <code> }
2

Use

XDocument.Load(Server.MapPath("~/XmlMetaData/Actions.1.xml"));

Comments

1

If the file is static, you might be better off embedding it and using Assembly.GetExecutingAssembly().GetManifestResourceStream().

1 Comment

local content files outside of resources in web application project are virtually always more efficient and easier to work with and can be deployed as a content type, not to mention you can get iis httpsys caching on them as well for outside accessed static content files.

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.