我是 LINQtoXML 的新手。我想使用 XElement.Load("") 方法。但编译器找不到我的文件。你能帮我为我的 XML 文件写正确的路径吗?请注意:我在 App_Code 中定义了一个类,我想在其中一种方法中使用 XML 文件数据,而我的 XML 文件位于 App_Data 中。
settings = XElement.Load("App_Data/AppSettings.xml");
我不能使用Request.ApplicationPath
和Page.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: