Sai Avinash
Sai Avinash

Reputation: 4753

Adding data to an existing xml file

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..

Upvotes: 0

Views: 4984

Answers (1)

Jasper
Jasper

Reputation: 534

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");

Upvotes: 3

Related Questions