XElement.Load(“~/App_Data/file.xml”) 找不到路径的一部分

发布于 2024-09-06 01:26:55 字数 2926 浏览 4 评论 0原文

我是 LINQtoXML 的新手。我想使用 XElement.Load("") 方法。但编译器找不到我的文件。你能帮我为我的 XML 文件编写正确的路径吗? 请注意:我在 App_Code 中定义了一个类,我想在其中一种方法中使用 XML 文件数据,而我的 XML 文件位于 App_Data 中。

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

我无法使用 Request.ApplicationPathPage.MapPath()Server.MapPath() 来获取我的文件的物理路径,因为我不在继承形式 Page 类中。

简要错误消息:< br> 找不到路径“C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\App_Data\AppSettings.xml”的一部分

你看到编译的路径与我的项目路径完全不同(G:\MyProjects\ASP.net Projects\VistaComputer\Website\App_Data\AppSettings.xml)

完整的错误消息在这里:

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: 

I am new in LINQtoXML. I want to use XElement.Load("") Method. but the compiler can't find my file. can you help me to write correct path for my XML file?
Note that: I defined a Class in App_Code and I want to use the XML file data in one of methods and my XML file Located in App_Data.

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

i cant Use Request.ApplicationPath and Page.MapPath() or Server.MapPath() to get the physical path for my file because i am not in a class Inherited form Page class.

Brief error Message:
Could not find a part of the path 'C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\App_Data\AppSettings.xml'.

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

Full error Message is 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: 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走野 2024-09-13 01:26:55

您可以尝试 HostingEnvironment.ApplicationPhysicalPath静态属性(假设这是在 ASP.NET 应用程序中使用):

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

我认为更好的方法是编写一个可重用的函数,该函数将文件名作为参数,并在一天结束时从某个 WebForm 中调用该函数,您将在其中调用可以访问Server.MapPath。这样做的优点是该函数不再依赖于 ASP.NET 引擎,并且可以在文件名计算方式不同的其他应用程序中重用。因此,基本上将关注点分开:

  1. 计算文件名位置
  2. 将文件名传递给对其进行一些解析的函数

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

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

I different and IMHO better approach is to write a reusable function which takes the filename as 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 is no longer dependent on the ASP.NET engine and could be reused in some other application where the filename will be calculated differently. So basically separate the concerns:

  1. Calculate the filename location
  2. Pass a filename to a function that does some parsing on it
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文