无法加载动态创建的XML文件.和Xml没有编码标签,我如何在创建Xml文件后添加根编码标签
本文关键字:创建 标签 文件 编码 Xml 添加 动态 加载 XML | 更新日期: 2023-09-27 17:50:34
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
}
您可以等一会儿再试一次。例如:
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次左右。
试试这个
string file = "<?xml version='"1.0'"?><mypathtoxml>" + File.ReadAllText(FILENAME) + "</mypathtoxml>";
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.LoadXml(file);