C# xmlDocument已被另一个进程使用
我创建了一个从XML文件读取和写入值的程序。当我试图用xmlDoc.save(path)保存XML时;它引发异常,因为该文件已被另一个进程使用 读取方法:C# xmlDocument已被另一个进程使用,c#,xml,winforms,C#,Xml,Winforms,我创建了一个从XML文件读取和写入值的程序。当我试图用xmlDoc.save(path)保存XML时;它引发异常,因为该文件已被另一个进程使用 读取方法: private void ReadXml() { //instantiate the xmlDocument xmlDoc = new XmlDocument(); //declare an XmlNodeList XmlNodeL
private void ReadXml()
{
//instantiate the xmlDocument
xmlDoc = new XmlDocument();
//declare an XmlNodeList
XmlNodeList xmlNode;
//create a FileStream to read the file
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
//load the file
xmlDoc.Load(fs);
//get all the nodes from the file
xmlNode = xmlDoc.GetElementsByTagName("Drop");
//read all values frome the nodes and save it into variables
for (int i = 0; i <= xmlNode.Count - 1; i++)
{
string name= xmlNode[i].ChildNodes.Item(0).InnerText.Trim();
int DefaultMin= Convert.ToInt32(xmlNode[i].ChildNodes.Item(1).InnerText.Trim());
int DefaultMax = Convert.ToInt32(xmlNode[i].ChildNodes.Item(2).InnerText.Trim());
int min = Convert.ToInt32(xmlNode[i].ChildNodes.Item(3).InnerText.Trim());
int max= Convert.ToInt32(xmlNode[i].ChildNodes.Item(4).InnerText.Trim());
int line= Convert.ToInt32(xmlNode[i].ChildNodes.Item(5).InnerText.Trim());
string lineToChange = xmlNode[i].ChildNodes.Item(6).InnerText;
string filePath = xmlNode[i].ChildNodes.Item(7).InnerText.Trim();
//create the DropItem object
DropItem drop = new DropItem(name, DefaultMin, DefaultMax, line, lineToChange, installPath+filePath);
drop.MinValue = min;
drop.MaxValue = max;
//add the object to the list
drops.Add(drop);
}
}
private void ReadXml()
{
//实例化xmlDocument
xmlDoc=新的XmlDocument();
//声明一个XmlNodeList
XmlNodeList xmlNode;
//创建文件流以读取文件
FileStream fs=newfilestream(路径,FileMode.Open,FileAccess.Read);
//加载文件
xmlDoc.Load(fs);
//从文件中获取所有节点
xmlNode=xmlDoc.GetElementsByTagName(“Drop”);
//从节点读取所有值并将其保存到变量中
对于(int i=0;i您没有关闭流,
使用Using
语句来处理和关闭FileStream
:
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
//load the file
xmlDoc.Load(fs);
//get all the nodes from the file
xmlNode = xmlDoc.GetElementsByTagName("Drop");
}
尝试添加到文件流
,这将有助于确保文件流
中的文件未被使用
public void WriteXml()
{
//declare a xmlNodeList
XmlNodeList xmlNode;
//create a FileStream to read the file
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
//load the file
xmlDoc.Load(fs);
//get all the nodes from the file
xmlNode = xmlDoc.GetElementsByTagName("Drop");
//write the values in the xml
for (int i = 0; i <= drops.Count - 1; i++)
{
xmlNode[i].ChildNodes.Item(3).InnerText=drops[i].MinValue.ToString();
xmlNode[i].ChildNodes.Item(4).InnerText = drops[i].MaxValue.ToString();
}
}
//save the document
xmlDoc.Save(path);
}
public void WriteXml()
{
//声明一个xmlNodeList
XmlNodeList xmlNode;
//创建文件流以读取文件
使用(FileStream fs=newfilestream(路径,FileMode.Open,FileAccess.Read))
{
//加载文件
xmlDoc.Load(fs);
//从文件中获取所有节点
xmlNode=xmlDoc.GetElementsByTagName(“Drop”);
//在xml中写入值
对于(int i=0;i尝试在ReadXml和WriteXml方法的末尾处理FileStream对象。尝试在每个一次性对象周围添加USE。它还将安全地关闭文件。
public void WriteXml()
{
//declare a xmlNodeList
XmlNodeList xmlNode;
//create a FileStream to read the file
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
//load the file
xmlDoc.Load(fs);
//get all the nodes from the file
xmlNode = xmlDoc.GetElementsByTagName("Drop");
//write the values in the xml
for (int i = 0; i <= drops.Count - 1; i++)
{
xmlNode[i].ChildNodes.Item(3).InnerText=drops[i].MinValue.ToString();
xmlNode[i].ChildNodes.Item(4).InnerText = drops[i].MaxValue.ToString();
}
}
//save the document
xmlDoc.Save(path);
}