asp.net mvc 4 - the process cannot access the file 'xxx.xml' because it is being used by another process -
i googled lot, dint solution problem. iam trying add node in xxx.xml file, throwing error "the process cannot access file 'xxx.xml' because being used process", below class
public class registration { list users; list newusers; string userpath = string.empty; string newuserpath = string.empty; string strusername = string.empty;
public bool findusername(string firstname, string lastname, string emailaddress, string country, string purchasedate, string username, string password) { //put code offers database 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 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 add node 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; } } }
thanks in advance...
it looks never closing reader, need call xr.close() @ point. or johan suggested, wrap in using statement:
using (xmlreader xr = xmlreader.create(userpath, xrs)) { //setting 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 add node 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; } }
also note: notice method named readxml, yet writing xml in method. can confusing, reading or writing? part of issue may opening file reading, , creating file writing?? have not dealt c# xml libs before doesn't seem right here. might consider breaking down more.
Comments
Post a Comment