4

私はLINQtoXMLが初めてです。XElement.Load("") メソッドを使用したい。しかし、コンパイラは私のファイルを見つけることができません。XML ファイルの正しいパスを書くのを手伝ってもらえますか? 注: App_Code でクラスを定義しました。メソッドの 1 つで XML ファイル データを使用し、App_Data にある XML ファイルを使用したいと考えています。

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

私はクラス継承フォームのページクラスにいないため、Request.ApplicationPathandPage.MapPath()またはを使用してファイルの物理パスを取得することはできません。Server.MapPath()

簡単なエラー メッセージ:
パス ' 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: 
4

1 に答える 1

12

HostingEnvironment.ApplicationPhysicalPath静的プロパティを試すことができます (これが ASP.NET アプリケーションで使用されていると仮定します)。

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

私とは異なり、IMHO のより良いアプローチは、ファイル名をパラメーターとして受け取り、1 日の終わりにアクセスできる Web フォームから呼び出される再利用可能な関数を作成することですServer.MapPath。これの利点は、この関数が ASP.NET エンジンに依存しなくなり、別の方法でファイル名が計算される他のアプリケーションで再利用できることです。したがって、基本的に懸念事項を分離します。

  1. ファイル名の場所を計算する
  2. ファイル名を解析する関数にファイル名を渡す
于 2010-06-17T16:37:28.180 に答える