Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I Want to convert xml files to Excel using asp .net c# so I am reading file as
xmlFile = XmlReader.Create("Product.xml", new XmlReaderSettings());



but It is giving error
{"Could not find file 'C:\\Program Files (x86)\\IIS Express\\Product.xml'.":"C:\\Program Files (x86)\\IIS Express\\Product.xml"}



My file is present in

C:\Program Files (x86)\IIS Express



also it is giving error at following location
releaseObject(xlApp);
 releaseObject(xlWorkBook);
releaseObject(xlWorkSheet);


does not exist in current context

What I have tried:

on button click I have following code to convert excel

protected void Button1_Click(object sender, EventArgs e)
      {
          Excel.Application xlApp;

          Excel.Workbook xlWorkBook;

          Excel.Worksheet xlWorkSheet;

          object misValue = System.Reflection.Missing.Value;

          DataSet ds = new DataSet();
          XmlReader xmlFile;
          int i = 0;
          int j = 0;
          xlApp = new Excel.ApplicationClass();
          xlWorkBook = xlApp.Workbooks.Add(misValue);
          xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);



          xmlFile = XmlReader.Create("Product.xml", new XmlReaderSettings());
          ds.ReadXml(xmlFile);
          for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
          {
              for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
              {
                  xlWorkSheet.Cells[i + 1, j + 1] = ds.Tables[0].Rows[i].ItemArray[j].ToString();
              }
          }
          xlWorkBook.SaveAs("D\\filename.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
          xlWorkBook.Close(true, misValue, misValue);
          xlApp.Quit();
          releaseObject(xlApp);
          releaseObject(xlWorkBook);
          releaseObject(xlWorkSheet);
          Response.Write("File has saved in D drive");
      }




also I want to know can I give another path for product.xml i.e can I put it on another drive and give that path how?

xmlFile = XmlReader.Create("Product.xml", new XmlReaderSettings());
Posted
Updated 14-Mar-18 22:08pm
v2
Comments
Dylvh 15-Mar-18 4:54am    
Try to put your file on another folder path and see if it helps. The programs files folder path is "protected", and you'll need to run your browser as an administrator to be able to access files from that location.
paul_vin 15-Mar-18 5:00am    
suppose I have put it on following path
D:\
then in following code what should be placed instead of "Product.xml"
xmlFile = XmlReader.Create("Product.xml", new XmlReaderSettings());


when I have Used as
using (XmlReader reader = XmlReader.Create(@"D:\VSTO_Vinayak\Product.xml"))
ds.ReadXml(reader);

it is giving exception as {"Could not find file 'D:\\VSTO_Vinayak\\Product.xml'.":"D:\\VSTO_Vinayak\\Product.xml"}
Dylvh 15-Mar-18 5:21am    
Normally it would be xmlFile = XmlReader.Create("D:\Product.xml", new XmlReaderSettings());. But you're doing it in asp.net, so you might need to read it in different way if that doesn't work.
Dylvh 15-Mar-18 5:31am    
I'm not an asp.net expert but can maybe guide you in a direction. Search on Google for "asp.net read file from disk". Hope it helps.
ZurdoDev 15-Mar-18 9:37am    
Whatever account your app pool is running as likely does not have permissions to read the drive there.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900