if (!File.Exists(locationFile))
{
string file = @"mypathtoxml";
XmlDocument objXmlDoc = new XmlDocument();
doc.Load(file); //- i get error here in loading my XML file which is created dynamically.
//process cannot access my path(my xml file Location) because it is being used by another process
}
答案 0 :(得分:0)
你可以等一会儿再试一次。例如:
var finished = false;
while (!finished)
{
try
{
if (!File.Exists(locationFile))
{
string file = @"mypathtoxml";
XmlDocument objXmlDoc = new XmlDocument();
doc.Load(file);
finished = true;
}
}
catch (IOException)
{
// the file is unavailable because it is:
// - still being written to
// - being processed by another thread
// so we just wait a second
Thread.Sleep(1000);
}
}
为了终止某个时间,您可以添加一个计数器,只尝试10次左右。
答案 1 :(得分:0)
试试这个
string file = "<?xml version=\"1.0\"?><mypathtoxml>" + File.ReadAllText(FILENAME) + "</mypathtoxml>";
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.LoadXml(file);