简体   繁体   English

ASP.NET Web应用程序无法找到我的XML文件,说它不在IIS Express文件夹中

[英]ASP.NET Web application cant find my XML file, sayis its not in IIS Express folder

I have in my another project a class which loads XML.xml file for XmlTextReader object. 我在我的另一个项目中有一个类,它为XmlTextReader对象加载XML.xml文件。 Then my another project(website) uses this project for loading xml file. 然后我的另一个项目(网站)使用此项目来加载xml文件。 So when I run this website it sayis it cant find XML.xml file inside C:\\Program Files(x86)\\IIS Express\\XML.xml . 因此,当我运行此网站时,它说它无法在C:\\ Program Files(x86)\\ IIS Express \\ XML.xml中找到XML.xml文件。 I cant create a new file there and I have no absolutely idea how to config this website to find my XML file from another directory. 我不能在那里创建一个新文件,我不知道如何配置这个网站从另一个目录中找到我的XML文件。 I tried to google a lot but couldnt find any good answers. 我试图谷歌很多,但无法找到任何好的答案。 Any fast fixes or something. 任何快速修复或其他东西。 Heres the code in asp.net 继承了asp.net中的代码

protected void Page_Load(object sender, EventArgs e)
    {
        XMLObjectParser parseri = new XMLObjectParser("XML.xml");
        parseri.readFileAndSave();
        item = parseri.getItem(1);

And code inside my XMLObjectParser 和我的XMLObjectParser中的代码

public XMLObjectParser(string file)
    {
        xmlreader = new XmlTextReader(file);
        this.file = file;

    }

Thanks 谢谢

Your code tries to open a file without specifying a path name. 您的代码尝试在不指定路径名的情况下打开文件。
In this scenario the resulting location of your file is the IIS process startup directory, not a folder inside you web site. 在此方案中,文件的结果位置是IIS进程启动目录,而不是您网站内的文件夹。

If you want to reach a file inside your web site folder you use Server.MapPath . 如果要访问网站文件夹中的文件,请使用Server.MapPath
As explained in the docs, if you don't set any relative path then the file is assumed to be present in the same folder where is located your ASP page. 如文档中所述,如果您未设置任何相对路径,则假定该文件存在于ASP页面所在的同一文件夹中。

So, you could write 所以,你可以写

 XMLObjectParser parseri = new XMLObjectParser(Server.MapPath("XML.xml"));

if your file should be located in the current folder relative to the calling page. 如果您的文件应位于相对于调用页面的当前文件夹中。
You can specify any folder inside the folder structure of your web site using something like this 您可以使用类似的方式在网站的文件夹结构中指定任何文件夹

 XMLObjectParser parseri = new XMLObjectParser(Server.MapPath("/APP_DATA/XML.xml"));

More info on Server.MapPath here 有关Server.MapPath的更多信息,请点击此处

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的 ASP.NET Web 应用程序在 IIS express 上正常运行,但在我发布它时却没有 - My ASP.NET Web application runs normally on IIS express but not when I publish it IIS Express Asp.Net正在使用该文件 - The file is in use by IIS Express Asp.Net ASP.NET Web 应用程序 -> Windows 身份验证 -> IIS Express -> Kerberos 还是 NTLM? - ASP.NET Web Application -> Windows Authentication -> IIS Express -> Kerberos or NTLM? 如何在本地IIS而不是Express中调试ASP.NET 5 Web应用程序 - How to debug an ASP.NET 5 web app in local IIS not Express 无法连接到 web 服务器 IIS Express - ASP.NET CORE - Unable to connect to web server IIS Express - ASP.NET CORE ASP.NET 5 MVC:无法连接到 Web 服务器“IIS Express” - ASP.NET 5 MVC: unable to connect to web server 'IIS Express' 在IIS / ASP.NET上的物理文件夹中创建虚拟应用程序/目录 - Creating virtual application/directory in physical folder on IIS/ASP.NET 如何在ASP.NET Web应用程序的App_Data文件夹中引用Sqlite db文件? - How do I reference the Sqlite db file in the App_Data folder for my ASP.NET Web Application? ASP.NET Core 3.1 Web 应用程序在 IIS Express 上运行时抛出错误 500.30,但在使用 dotnet watch run 时不会抛出错误 - ASP.NET Core 3.1 web application throws error 500.30 when run on IIS Express, but not when using dotnet watch run 如何在本地iis上部署asp.net Web应用程序? - How can I deploy an asp.net web application on my local iis?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM