I’m trying to retrieve data from an Xml. I’m a newbie into programming so please pardon me.
protected void Page_Load(object sender, EventArgs e)
{
string MyXmlFile= @"E:\\Programming stuff\\Work\\website\\XMLFile.xml";
DataSet ds= new DataSet();
System.IO.FileStream MyReadXml= new System.IO.FileStream(MyXmlFile, System.IO.FileMode.Open);
ds.ReadXml(MyReadXml);
DataGrid DataGrid1 = new DataGrid();
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}
The error I get on the browser is:
“The process cannot access the file ‘E:\Programming stuff\Work\website\XMLFile.xml’ because it is being used by another process.”
Can you help me identify which is that other process that is accessing the file?
Edit: After the changes to the code:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
string MyXmlFile= Server.MapPath("~/XMLFile.xml");
using(System.IO.FileStream MyReadXml= new System.IO.FileStream(MyXmlFile,System.IO.FileMode.Open));
{
DataSet ds= new DataSet();
ds.ReadXml(MyReadXml);
DataGrid DataGrid1 = new DataGrid();
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
PlaceHolder1.Controls.Add(DataGrid1);
}
}
}
Error: “The name ‘MyReadXml’ does not exist in the current context”
Always
closeanddisposethe stream (try using block). restart your appserver (werserver) and see what happen?NOTE: If
XMLFile.xmlis placed under the root ofwebsitethen useServer.MapPath()method to get absolute file path from virtual path.If you want to add ASP.NET server control programatically then add
PlaceHoldercontrol into .aspx file and call thePlaceControl1.Controls.Add(DataGrid1)method.EDIT:
You’ve terminated the using block. Please remove the semi-colon.