XElement.Load ("~ / App_Data / file.xml") Could not find part of the path

I am new to LINQtoXML. I want to use the XElement.Load ("") method. but the compiler cannot find my file. can you help me write the correct path for my xml file? Please note: I defined the class in App_Code, and I want to use the XML file data in one of the methods and in my XML file. Located in App_Data.

settings = XElement.Load("App_Data/AppSettings.xml"); 

i can not Use Request.ApplicationPath and Page.MapPath() or Server.MapPath() to get the physical path for my file because I'm not in the class. Inherited form. Page class.

Short error message:
Could not find part of the path C: \ Program Files \ Microsoft Visual Studio 9.0 \ Common7 \ IDE \ App_Data \ AppSettings.xml .

you see that the compiled path is completely different from my project path (G: \ MyProjects \ ASP.net Projects \ VistaComputer \ Website \ App_Data \ AppSettings.xml)

Full error message here:

 System.IO.DirectoryNotFoundException was unhandled by user code Message="Could not find a part of the path 'C:\\Program Files\\Microsoft Visual Studio 9.0\\Common7\\IDE\\App_Data\\AppSettings.xml'." Source="mscorlib" StackTrace: at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize) at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials) at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn) at System.Xml.XmlReader.Create(String inputUri, XmlReaderSettings settings, XmlParserContext inputContext) at System.Xml.XmlReader.Create(String inputUri, XmlReaderSettings settings) at System.Xml.Linq.XElement.Load(String uri, LoadOptions options) at System.Xml.Linq.XElement.Load(String uri) at ProductActions.Add(Int32 catId, String title, String price, String website, String shortDesc, String fullDesc, Boolean active, Boolean editorPick, String fileName, Stream image) in g:\MyProjects\ASP.net Projects\VistaComputer\Website\App_Code\ProductActions.cs:line 67 at CMS_Products_Operations.Button1_Click(Object sender, EventArgs e) in g:\MyProjects\ASP.net Projects\VistaComputer\Website\CMS\Products\Operations.aspx.cs:line 72 at System.Web.UI.WebControls.Button.OnClick(EventArgs e) at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) InnerException: 
+4
source share
1 answer

You can try the static property HostingEnvironment.ApplicationPhysicalPath (assuming it is used in an ASP.NET application):

 string filePath = Path.Combine( HostingEnvironment.ApplicationPhysicalPath, @"App_Data\AppSettings.xml" ); 

I am different, and IMHO's best approach is to write a reusable function that takes a file name as a parameter and which at the end of the day will be called from some WebForm where you will have access to Server.MapPath . The advantage of this is that this function no longer depends on the ASP.NET mechanism and can be reused in another application, where the file name will be calculated differently. Therefore, the main problems are separated:

  • Calculate file name location
  • Pass the file name of the function that parses it
+12
source

Source: https://habr.com/ru/post/1313095/


All Articles