Click here to Skip to main content
15,896,730 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I am creating an application where i am reading an xml,Adding some nodes and then saving it.

While saving i am getting following error.

"The process cannot access the file '"C:\p401573\myxml.xml"' because it is being used by another process."

My code is as follows
C#
Dim xmldoc As New XmlDocument
xmldoc.Load("C:\p401573\myxml.xml")
'ConfigurationManager.AppSettings("Path") + "
Dim nlist As XmlNodeList = xmldoc.GetElementsByTagName("UserSettings")
Dim elem As XmlElement = xmldoc.CreateElement("", "User", "")
elem.SetAttribute("group", cbxUserProfile.SelectedItem)
elem.SetAttribute("Rights", "N")
nlist.Item(0).AppendChild(elem)
xmldoc.Save("C:\p401573\myxml.xml")


On last line of code when i am saving xml document back to the file above error comes.Some times this piece of code is working

myxml.xml file code
XML
<?xml version="1.0" encoding="utf-8"?>
<UserSettings>
    <User Name="vijavaib" SSOId="45699795" group="New Business" Rights="" />
    <User Name="girinity" SSOId="757888" group="PHS" Rights="" />
    <User Name="bandalpa" SSOId="64567788" group="LICENSING" Rights="" />
    <User Name="hjdhjj" SSOId="658885858" group="PHS" Rights="" />
    <User Name="vvv" SSOId="3165667" group="PHS" Rights="" />
    <User Name="cxvbbb" SSOId="5614367" group="PHS" Rights="" />
    <User Name="cxvbbb" SSOId="5614367" group="LICENSING" Rights="" />
    <User Name="cxvbbb" SSOId="5614367" group="LTCNB" Rights="" />
    <User Name="" SSOId="" group="ALL" Rights="" />
    <User Name="gvbfgfd" SSOId="423556767" group="New Business" Rights="N" />
    <User Name="tgytyry" SSOId="654657676" group="New Business" Rights="N" />
    <User Name="yuuyuut" SSOId="546546757" group="New Business" Rights="N" />
    <User Name="fhgffgjfj" SSOId="576575565" group="New Business" Rights="N" />
    <User Name="hjhjghdj67658" SSOId="654776787" group="New Business" Rights="N" />
    <User Name="hyjhjhgj" SSOId="677887789" group="New Business" Rights="N" />
    <User Name="ttioioiuo" SSOId="787589980" group="ALL" Rights="N" />
  </UserSettings>



But some times it give error.

Please Help its urgent.
Posted
Updated 13-Mar-15 0:21am
v3
Comments
Leo Chapiro 13-Mar-15 6:09am    
Uncleare on incomplete: your source code doesn't make any sense, please update it
vaibhav vijay 13-Mar-15 6:22am    
I have updated xml also with question please check.

1 solution

 
Share this answer
 
Comments
vaibhav vijay 13-Mar-15 6:23am    
How to fix this issue what should be the alternate way .
I can't change the file read and save location every time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900