无法加载动态创建的XML文件。而没有编码标签的Xml如何在创建XML文件后添加根编码标签

时间:2021-12-23 05:58:48
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
}

2 个解决方案

#1


0  

You could wait a while and try it again. For example something like this:

你可以等一会儿再试一次。例如这样的事情:

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);
  }
}

In order to terminate sometime, you could add a counter and only try it 10 times or so.

为了终止某个时间,你可以添加一个计数器,只尝试10次左右。

#2


0  

Try this

            string file = "<?xml version=\"1.0\"?><mypathtoxml>" + File.ReadAllText(FILENAME) +  "</mypathtoxml>";
            XmlDocument objXmlDoc = new XmlDocument();
            objXmlDoc.LoadXml(file); ​​

#1


0  

You could wait a while and try it again. For example something like this:

你可以等一会儿再试一次。例如这样的事情:

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);
  }
}

In order to terminate sometime, you could add a counter and only try it 10 times or so.

为了终止某个时间,你可以添加一个计数器,只尝试10次左右。

#2


0  

Try this

            string file = "<?xml version=\"1.0\"?><mypathtoxml>" + File.ReadAllText(FILENAME) +  "</mypathtoxml>";
            XmlDocument objXmlDoc = new XmlDocument();
            objXmlDoc.LoadXml(file); ​​