How to save changes to XmlDataDocument?

D

deko

I create an XmlDataDocument Dataset when the main form of my WinForms app
opens. The user will make changes to the Dataset, and then those changes
should be saved to the XML file when the code runs xdd.Save(xmlFile). But I
get an error when I try to save the Dataset back to the XML file: "The
process cannot access the file "C:\Projects.xml" because it is being used by
another process." Is this because I'm in Debug mode? How do I persist
changes to the XML file? Thanks in advance.

public class frmMain : System.Windows.Forms.Form
{
string xmlSchema = (@"C:\Projects.xsd");
string xmlFile = (@"C:\Projects.xml");
//[code omitted]

XmlDataDocument xdd = new XmlDataDocument();

public frmMain()
{
xdd.DataSet.ReadXmlSchema(xmlSchema);
XmlTextReader xrd = new XmlTextReader(xmlFile);
xrd.MoveToContent();
xdd.Load(xrd);
}

private void btnAddCfg_Click(object sender, System.EventArgs e)
{
DataTable someTbl = xdd.DataSet.Tables["Configuration"];
DataRow newCfg = someTbl.NewRow();
newCfg["ConfigName"] = "NEW CONFIGURATION";
prjTbl.Rows.Add(newCfg);
dsDebug.DebugWriteLine(xdd.DataSet);
//xdd.Save(xmlFile); //does not work // <<== * * ERROR HERE
//error rec'd says can't save while file is open
//but there must be a way to persist the changes
//is the error because I'm in debug mode and VS is open?
}
}
 
D

deko

Try closing your Xml object(s).

Thanks for the reply. I am kind of green... can you provide an example of
how to do this?

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top