Asp.net mvc 4 进程无法访问文件';xxx.xml';因为它正被另一个进程使用
我在谷歌上搜索了很多,但我无法找到任何解决问题的方法。 我正在尝试将节点添加到xxx.xml文件中,但抛出了一个错误 “进程无法访问文件'xxx.xml',因为它正被另一个进程使用””,下面是我的类 公开课注册 { 列出用户名单; 列出新用户; string Userpath=string.Empty; string NewUserpath=string.Empty; string strUsername=string.EmptyAsp.net mvc 4 进程无法访问文件';xxx.xml';因为它正被另一个进程使用,asp.net-mvc-4,Asp.net Mvc 4,我在谷歌上搜索了很多,但我无法找到任何解决问题的方法。 我正在尝试将节点添加到xxx.xml文件中,但抛出了一个错误 “进程无法访问文件'xxx.xml',因为它正被另一个进程使用””,下面是我的类 公开课注册 { 列出用户名单; 列出新用户; string Userpath=string.Empty; string NewUserpath=string.Empty; string strUsername=string.Empty public bool FINDUSERNAME(str
public bool FINDUSERNAME(string firstname, string lastname, string emailaddress, string country, string purchasedate, string username, string password)
{
//Put code to get the offers from database to Offers variable
if (ReadXML(firstname, lastname, emailaddress, country, purchasedate, username, password))
return true;
else
return false;
}
//bool ReadXML(XmlDocument xmlfile2)
bool ReadXML(string firstname, string lastname, string emailaddress, string country, string purchasedate, string username, string password)
{
try
{
XmlDocument receivedxml = new XmlDocument();
Userpath = HttpContext.Current.Server.MapPath("/SampleData/Registration.xml");
NewUserpath = HttpContext.Current.Server.MapPath("/SampleData/NewRegistration.xml");
XmlReaderSettings xrs = new XmlReaderSettings();
xrs.DtdProcessing = DtdProcessing.Ignore;
XmlReader xr = XmlReader.Create(Userpath, xrs);
if (xr != null)
{
//Setting the Root element
XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "Registration";
xRoot.IsNullable = true;
XmlSerializer deserializer = new XmlSerializer(typeof(Registration), xRoot);
Registration UserDetails = (Registration)deserializer.Deserialize(xr);
Users = UserDetails.Users;
foreach (var varuser in Users)
{
if (username == varuser.Username)
{
strUsername = varuser.Username;
return true;
}
}
if (strUsername == "")
{
//here iam trying to add a node to the xml
using (StreamWriter sw = new StreamWriter(File.Create(Userpath)))
{
sw.Write("<User><Firstname>"
+ firstname + "</Firstname><Lastname>"
+ lastname + "</Lastname><Country>"
+ country + "</Country><Purchasedate>"
+ purchasedate + "</Purchasedate><Emailaddress>"
+ emailaddress + "</Emailaddress><Username>"
+ username + "</Username><Password>"
+ password + "</Password></User>");
}
return false;
}
}
return false;
}
catch (Exception)
{
return false;
}
}
}
public bool FINDUSERNAME(字符串firstname、字符串lastname、字符串emailaddress、字符串country、字符串purchasedate、字符串用户名、字符串密码)
{
//将从数据库获取报价的代码放入报价变量
if(ReadXML(名字、姓氏、电子邮件地址、国家/地区、购买日期、用户名、密码))
返回true;
其他的
返回false;
}
//bool ReadXML(XmlDocument xmlfile2)
bool ReadXML(字符串firstname、字符串lastname、字符串emailaddress、字符串country、字符串purchasedate、字符串用户名、字符串密码)
{
尝试
{
XmlDocument receivedxml=新的XmlDocument();
Userpath=HttpContext.Current.Server.MapPath(“/SampleData/Registration.xml”);
NewUserpath=HttpContext.Current.Server.MapPath(“/SampleData/NewRegistration.xml”);
XmlReaderSettings xrs=新的XmlReaderSettings();
xrs.DtdProcessing=DtdProcessing.Ignore;
XmlReader xr=XmlReader.Create(Userpath,xrs);
如果(xr!=null)
{
//设置根元素
xRoot=新的XmlRootAttribute();
xRoot.ElementName=“注册”;
xRoot.IsNullable=true;
XmlSerializer反序列化器=新的XmlSerializer(typeof(Registration),xRoot);
注册UserDetails=(注册)反序列化程序。反序列化(xr);
Users=UserDetails.Users;
foreach(用户中的var varuser)
{
if(username==varuser.username)
{
strUsername=varuser.Username;
返回true;
}
}
如果(strUsername==“”)
{
//在这里,我试图向xml添加一个节点
使用(StreamWriter sw=newstreamwriter(File.Create(Userpath)))
{
sw.写(“”)
+名字+“”
+姓氏+“”
+国家+“”
+purchasedate+“”
+电子邮件地址+“”
+用户名+“”
+密码+“”);
}
返回false;
}
}
返回false;
}
捕获(例外)
{
返回false;
}
}
}
提前感谢…看起来您永远不会关闭读卡器,您需要在某个时候调用xr.Close()。或者按照Johan的建议,用using语句将其包装起来:
using (XmlReader xr = XmlReader.Create(Userpath, xrs))
{
//Setting the Root element
XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "Registration";
xRoot.IsNullable = true;
XmlSerializer deserializer = new XmlSerializer(typeof(Registration), xRoot);
Registration UserDetails = (Registration)deserializer.Deserialize(xr);
Users = UserDetails.Users;
foreach (var varuser in Users)
{
if (username == varuser.Username)
{
strUsername = varuser.Username;
return true;
}
}
if (strUsername == "")
{
//here iam trying to add a node to the xml
using (StreamWriter sw = new StreamWriter(File.Create(Userpath)))
{
sw.Write("<User><Firstname>"
+ firstname + "</Firstname><Lastname>"
+ lastname + "</Lastname><Country>"
+ country + "</Country><Purchasedate>"
+ purchasedate + "</Purchasedate><Emailaddress>"
+ emailaddress + "</Emailaddress><Username>"
+ username + "</Username><Password>"
+ password + "</Password></User>");
}
return false;
}
}
使用(XmlReader xr=XmlReader.Create(Userpath,xrs))
{
//设置根元素
xRoot=新的XmlRootAttribute();
xRoot.ElementName=“注册”;
xRoot.IsNullable=true;
XmlSerializer反序列化器=新的XmlSerializer(typeof(Registration),xRoot);
注册UserDetails=(注册)反序列化程序。反序列化(xr);
Users=UserDetails.Users;
foreach(用户中的var varuser)
{
if(username==varuser.username)
{
strUsername=varuser.Username;
返回true;
}
}
如果(strUsername==“”)
{
//在这里,我试图向xml添加一个节点
使用(StreamWriter sw=newstreamwriter(File.Create(Userpath)))
{
sw.写(“”)
+名字+“”
+姓氏+“”
+国家+“”
+purchasedate+“”
+电子邮件地址+“”
+用户名+“”
+密码+“”);
}
返回false;
}
}
还有另一个注意事项:我注意到您的方法名为ReadXML,但您也在用这个方法编写XML。这可能会让人困惑,你是在读还是在写?您的部分问题还可能是您正在打开文件进行读取,然后创建文件进行写入??我以前没有处理过C#Xml库,但这里似乎有些不对劲。你可能会考虑更多地打破这一点。看起来是一个很好的理由让你的读者参与其中。