R
rao
Hi All,
I am generating temporary xml files to bind it to tree view control.
A unique xml file is generated for each user. I generating these files
Using streamwriter class. Later when I try to delete the file with
File.Dlete in page unload event I gets an error File already in use by
another process. When I examine process explorer aspnet_wp.exe is hold
these files.
Here is the code I am working on.. Any advise please.
Thanks in advance
Rao
private void Page_UnLoad(object sender, System.EventArgs e)
{
try
{
if (File.Exists(Server.MapPath(xmlDocPath+ProjectsDoc)))
{ ProjectTree.Dispose();
File.Delete(Server.MapPath(xmlDocPath+ProjectsDoc));
}
}
catch(Exception ex)
{SetErrorMessage(ex.Message.ToString(),true);
}
}
private void InitializeTreeView()
{
//Add data to treeview based on the projectXML stored procedure
//ProjectTree.TreeNodeSrc=
DataRetrieval.formatXMLforTree("eacollin.ProjectXML");
ProjectsDoc=DataRetrieval.PrepareProjectsXMLdoc("eacollin.ProjectXML");
ProjectTree.TreeNodeSrc= Server.MapPath(xmlDocPath+ProjectsDoc);
ProjectTree.DefaultStyle.CssText="color: black;font-family:
verdana;font-size: 8pt;";
ProjectTree.DataBind();
ProjectTree.Dispose();
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
this.Unload+=new System.EventHandler(this.Page_UnLoad);
//ProjectTree.Unload+=new System.EventHandler(this.DeleteXmlFiles);
}
public static string PrepareProjectsXMLdoc(string SPname)
{
// added by rao 8/27/2004
// added projects cache to avoid round trip to sql server, and
// additional processing involved in process returned xml from
// stored procedure
string strXML;
strXML="";
DataSet ds=new DataSet();
string OutFileName="";
System.IO.StringWriter strWriter = new StringWriter();
try
{
if(System.Web.HttpContext.Current.Cache["Projects"]==null)
{
ds = SqlHelper.ExecuteDataset(ConfigurationSettings.AppSettings[ASPNET.StarterKit.TimeTracker.Web.Global.CfgKeyConnString],
SPname);
strXML = "";
ds.WriteXml(strWriter);
strXML = strWriter.ToString();
//get rid of the text version of < and > tags
strXML = Regex.Replace(strXML, "<", "<");
strXML = Regex.Replace(strXML, ">", ">");
//get ride of the XML headers
string search =
@"</XML_F52E2B61-18A1-11d1-B105-00805F49916B>\s*</Table>\s*<Table>\s*<XML_F52E2B61-18A1-11d1-B105-00805F49916B>";
strXML = Regex.Replace(strXML,search,"");
//Get rid of apostrophes which do not show up properly on the
webpage
strXML = Regex.Replace(strXML,"&apos;","'");
//find the part of the xml that starts and ends with TREENODES
int firstoccurance = strXML.IndexOf("TREENODES");
int secondoccurance =
strXML.IndexOf("TREENODES",firstoccurance+1);
int delta = secondoccurance - firstoccurance;
strXML = strXML.Substring(firstoccurance,delta);
strXML = "<" + strXML + "TREENODES>";
ds.Dispose();
// generate uniqe filename for each user
lock(System.Web.HttpContext.Current.Cache)
{
System.Web.HttpContext.Current.Cache.Insert("Projects",strXML,null,
DateTime.Now.AddHours(HoursLeft),Cache.NoSlidingExpiration);
}
}
else
{
strXML =(string)System.Web.HttpContext.Current.Cache["Projects"];
}
string NewGuid = System.Guid.NewGuid().ToString();
OutFileName=NewGuid+"_Projects.xml";
using(StreamWriter wr = new
StreamWriter(System.Web.HttpContext.Current.Server.MapPath(xmlDocPath+OutFileName)))
{
wr.Write(strXML);
}
strWriter.Close();
//return OutFileName;
}
catch(Exception ex)
{
System.Web.HttpContext.Current.Response.Write(ex.Message);
}
return OutFileName;
}
I am generating temporary xml files to bind it to tree view control.
A unique xml file is generated for each user. I generating these files
Using streamwriter class. Later when I try to delete the file with
File.Dlete in page unload event I gets an error File already in use by
another process. When I examine process explorer aspnet_wp.exe is hold
these files.
Here is the code I am working on.. Any advise please.
Thanks in advance
Rao
private void Page_UnLoad(object sender, System.EventArgs e)
{
try
{
if (File.Exists(Server.MapPath(xmlDocPath+ProjectsDoc)))
{ ProjectTree.Dispose();
File.Delete(Server.MapPath(xmlDocPath+ProjectsDoc));
}
}
catch(Exception ex)
{SetErrorMessage(ex.Message.ToString(),true);
}
}
private void InitializeTreeView()
{
//Add data to treeview based on the projectXML stored procedure
//ProjectTree.TreeNodeSrc=
DataRetrieval.formatXMLforTree("eacollin.ProjectXML");
ProjectsDoc=DataRetrieval.PrepareProjectsXMLdoc("eacollin.ProjectXML");
ProjectTree.TreeNodeSrc= Server.MapPath(xmlDocPath+ProjectsDoc);
ProjectTree.DefaultStyle.CssText="color: black;font-family:
verdana;font-size: 8pt;";
ProjectTree.DataBind();
ProjectTree.Dispose();
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
this.Unload+=new System.EventHandler(this.Page_UnLoad);
//ProjectTree.Unload+=new System.EventHandler(this.DeleteXmlFiles);
}
public static string PrepareProjectsXMLdoc(string SPname)
{
// added by rao 8/27/2004
// added projects cache to avoid round trip to sql server, and
// additional processing involved in process returned xml from
// stored procedure
string strXML;
strXML="";
DataSet ds=new DataSet();
string OutFileName="";
System.IO.StringWriter strWriter = new StringWriter();
try
{
if(System.Web.HttpContext.Current.Cache["Projects"]==null)
{
ds = SqlHelper.ExecuteDataset(ConfigurationSettings.AppSettings[ASPNET.StarterKit.TimeTracker.Web.Global.CfgKeyConnString],
SPname);
strXML = "";
ds.WriteXml(strWriter);
strXML = strWriter.ToString();
//get rid of the text version of < and > tags
strXML = Regex.Replace(strXML, "<", "<");
strXML = Regex.Replace(strXML, ">", ">");
//get ride of the XML headers
string search =
@"</XML_F52E2B61-18A1-11d1-B105-00805F49916B>\s*</Table>\s*<Table>\s*<XML_F52E2B61-18A1-11d1-B105-00805F49916B>";
strXML = Regex.Replace(strXML,search,"");
//Get rid of apostrophes which do not show up properly on the
webpage
strXML = Regex.Replace(strXML,"&apos;","'");
//find the part of the xml that starts and ends with TREENODES
int firstoccurance = strXML.IndexOf("TREENODES");
int secondoccurance =
strXML.IndexOf("TREENODES",firstoccurance+1);
int delta = secondoccurance - firstoccurance;
strXML = strXML.Substring(firstoccurance,delta);
strXML = "<" + strXML + "TREENODES>";
ds.Dispose();
// generate uniqe filename for each user
lock(System.Web.HttpContext.Current.Cache)
{
System.Web.HttpContext.Current.Cache.Insert("Projects",strXML,null,
DateTime.Now.AddHours(HoursLeft),Cache.NoSlidingExpiration);
}
}
else
{
strXML =(string)System.Web.HttpContext.Current.Cache["Projects"];
}
string NewGuid = System.Guid.NewGuid().ToString();
OutFileName=NewGuid+"_Projects.xml";
using(StreamWriter wr = new
StreamWriter(System.Web.HttpContext.Current.Server.MapPath(xmlDocPath+OutFileName)))
{
wr.Write(strXML);
}
strWriter.Close();
//return OutFileName;
}
catch(Exception ex)
{
System.Web.HttpContext.Current.Response.Write(ex.Message);
}
return OutFileName;
}