Last post Jun 11, 2011 05:45 PM by Slipperman
Jun 11, 2011 02:56 PM|Slipperman|LINK
i found a few articles on the internet that seem to be showing exactly what i want to do but thier code just won't work in my app. here's the background..
i'm working on an application that uses an xml file as it's database. up to this point, there was no delete capability in the app which consequently over time has allowed the file to grow to such a size where it drags down the application. because of the
performance issues, we decided to implement the delete functionality in the app but only on a limited basis (only 3 users will have access to the Delete button which is disabled for everyone else). that code is written and works fine but i decided that for
better data protection that the record (xmlnode) they choose to delete is first copied into a backup file before its deleted from the main file. obviously since this backup file won't exist the first time a chosen node is copied, i have to create the file
first. this is what i think differentiates my code from the stuff i saw on the internet and probably why it doesn't work. this is what i'm doing..
in Delete event handler..
doc.Load(path); // load main db file
XmlNode node = doc.SelectSingleNode(@"//*[@ID='" + jobIDint + "']"); // select particular job id to copy/delete
CopyNodeToBackup(node); // pass it into Copy function
private void CopyNodeToBackup(XmlNode node)
XmlTextWriter writer = new XmlTextWriter(backup, System.Text.Encoding.Default);
// //Use indenting for readability
writer.Formatting = Formatting.Indented;
// //Write the XML declaration
// ////Write an element (this one is the root)
XmlDocument destDoc = new XmlDocument();
XmlNode tempNode = destDoc.ImportNode(node, true);
destDoc.FirstChild.AppendChild(tempNode); // <<<--------------- blows up
// FirstChild error: The current node cannot contain other nodes.
// straight AppendChild: This document already has a 'DocumentElement' node.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jobtitle>Associate Director Mobile Development</jobtitle>
<location>White Plains, NY </location>
<salary>115 K to 120 k Plus Bonus</salary>
<description> Job description goes here</description>
Jun 11, 2011 05:45 PM|Slipperman|LINK
i found it.. i accidentally discovered that DocumentElement also has an AppendChild method. when i used that instead of FirstChild, it worked. i also didn't have to include a Write element for 'Job'. all's this did was write out a dummy child node which