vb.net - Closing an XML file after reading so it can be deleted -


i have problem deleting xml file after loading .xmldocument.

my code parses xml file specific nodes , allocates values variables.

once complete code processes data based on values xml file.

this works fine until end when try delete xml file still open , error "the process cannot access file because being used process" guess xmldocument reader.

here section of the xml processing code - works fine.

    `dim xmldoc xmldocument = new xmldocument()      xmldoc.load(strfilename)     intpassed = xmldoc.selectsinglenode("//calibrationpassed").innertext     boolcheck = xmldoc.selectsinglenode("//checkscomplete").innertext     intcertrequired = xmldoc.selectsinglenode("//schedule").innertext     console.writeline("calibration passed: " & intpassed)     console.writeline("checks complete:" & boolcheck)     console.writeline("schedule: " & intcertrequired)       strfirstname = xmldoc.selectsinglenode("//firstname").innertext     stremail = xmldoc.selectsinglenode("//email").innertext     strcusemail = xmldoc.selectsinglenode("//customeremail").innertext     strcompanyname = xmldoc.selectsinglenode("//companyname").innertext     strcontractnumber = xmldoc.selectsinglenode("//contractno").innertext      console.writeline("first name: " & strfirstname)     console.writeline("email: " & stremail)     console.writeline("customer email: " & strcusemail)     console.writeline("company name: " & strcompanyname)     console.writeline("contract no: " & strcontractnumber)      console.writeline("xml parsing complete") 

` code being used delete file is:

if system.io.file.exists(strfilename) = true                 system.io.file.delete(strfilename)                 console.writeline("deleted xml file")             end if 

any on i'm going wrong great-fully received.

thanks

xmldocument.load uses stream reader under hood. there 2 strategies avoiding this:

1) using block close/dispose stream automatically , promptly

using xmldoc xmldocument = new xmldocument()     xmldoc.load(strfilename)     'all of copying stuff end using 'now delete file 

2) load xml , avoid using reader:

dim strxml string strxml = system.io.file.readalltext(strfilename) dim xmldoc xmldocument = new xmldocument() xmldoc.loadxml(strxml) 'and rest of code 

the downside 2nd approach example doesn't consider other encoding, should past current problem. dealing various encoding options whole different matter.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -