0

I am using the follwing syntax to add data to already existing xml file in the following way:

XmlTextReader Reader = new XmlTextReader("ServerPaths.xml");
        DataSet dsNewList = new DataSet();
        dsNewList.ReadXml(Reader);
        Reader.Close();
        DataTable dt = dsNewList.Tables[0];
        dt.Rows.Add(txtNewServerPath.Text);
        dt.WriteXml("ServerPaths.xml",false);

But, i was getting error at last line as:

System.IO.IOException was unhandled
          Message=The process cannot access the file 'C:\Documents and Settings\590000\my documents\visual studio 2010\Projects\EasyDeployer\EasyDeployer\bin\Debug\ServerPaths.xml' because it is being used by another process

Please help in solving this error. or is there any other way to do this?

My xml file looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<ServerList>
<ServerPath>C:\\Avinash\\Dev1</ServerPath>
<ServerPath>C:\\Avinash\\Dev1</ServerPath>
</ServerList>

I just wanted to add new serverpath..

6
  • Maybe try Reader.Flush(); Commented Sep 20, 2013 at 13:56
  • Do you have the file open while you're running this? Commented Sep 20, 2013 at 13:57
  • Reader.Flush() is not available Commented Sep 20, 2013 at 13:58
  • No..The file is closed. Commented Sep 20, 2013 at 13:59
  • Try Reader.Dispose() after you're done reading the file. Commented Sep 20, 2013 at 14:00

1 Answer 1

3

You could also try adding XmlNode's to the xml document like this:

XmlDocument xd = new XmlDocument();
xd.Load("ServerPaths.xml");

XmlNode rootNode = xd.DocumentElement;
XmlNode serverPathNode = xd.CreateElement("ServerPath");
serverPathNode.InnerText = txtNewServerPath.Text; // Your value

rootNode.AppendChild(serverPathNode);

xd.Save("ServerPaths.xml");
Sign up to request clarification or add additional context in comments.

6 Comments

Do you have the XML document open somewhere else? Like an editor?
The file is in the debug folder of the applciation, is it creating any problem?
No,it is completely closed
It's somehow locked, are you using this file in any other way in your code? And maybe not closing the connection? You could also try using another file.
Got..it..Thanks. yes it was opened in other part of code..now i closed it by using Reader.close()
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.