Pergunta

I have in my another project a class which loads XML.xml file for XmlTextReader object. Then my another project(website) uses this project for loading xml file. So when I run this website it sayis it cant find XML.xml file inside C:\Program Files(x86)\IIS Express\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. I tried to google a lot but couldnt find any good answers. Any fast fixes or something. Heres the code in 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

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

    }

Thanks

Foi útil?

Solução

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.

If you want to reach a file inside your web site folder you use 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.

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top