[英]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.