2010-06-17 115 views
4

我是LINQtoXML中的新成員。我想使用XElement.Load(「」)方法。但編譯器無法找到我的文件。你能幫我寫出我的XML文件的正確路徑嗎? 請注意:我在App_Code中定義了一個類,我想在其中一個方法中使用XML文件數據,並且我的XML文件位於App_Data中。
XElement.Load(「〜/ App_Data/file.xml」)找不到路徑的一部分

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

我不能使用Request.ApplicationPathPage.MapPath()Server.MapPath(),因爲我在一個類繼承的形式Page類是無法得到的物理路徑爲我的文件。

簡短的錯誤消息:
找不到路徑 'C:\ Program Files文件\微軟的Visual Studio 9.0 \ Common7 \ IDE \ App_Data文件\ AppSettings.xml' 的一部分

看到編譯的路徑是從我的項目路徑完全不同的(G:\ MyProjects下\ ASP.net項目\ VistaComputer \網站\ 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: 

回答

12

你可以嘗試HostingEnvironment.ApplicationPhysicalPath靜態屬性(假設這在ASP.NET應用程序使用):

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

我不同,恕我直言,更好的方法是編寫一個可重複使用的函數,它將文件名作爲參數,並在一天結束時從某個WebForm中調用,您將有權訪問Server.MapPath。這樣做的好處是,該函數不再依賴於ASP.NET引擎,並且可以在其他應用程序中重用,其中文件名的計算方式不同。所以基本上分開的擔憂:

  1. 計算文件名的位置
  2. 傳遞一個文件名,做它
+0

非常感謝你的一些解析功能。很有幫助。 – mahdiahmadirad 2010-06-17 19:38:49

+0

+1謝謝!那正是我想要的。 – devios1 2010-07-02 17:12:59

相關問題