0

I have a class XMLProfile which has the methods to write/edit/delete data into the xml file. This data is read and displayed in a listview in Visual C#

public void xmlwriter(string path)
        {
            XmlDocument xdoc = new XmlDocument();                
            xdoc.Load("C:\\product.txt);
            XmlNode fold = xdoc.CreateElement("Folder");
            XmlNode name = xdoc.CreateElement("Name");
            XmlNode rec = xdoc.CreateElement("Recurse");
            name.InnerText = path;
            rec.InnerText = "true";
            fold.AppendChild(name);
            fold.AppendChild(rec);
            xdoc.SelectSingleNode("//Folders").AppendChild(fold);
            xdoc.Save("C:\\product.txt");
        }

The write method (Adding new data to the xml) is shown above and the delete is shown below

public void delete(string snode)
        {
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load("C:\\product.txt");
            foreach (XmlNode node in xdoc.SelectNodes("BackupProfile/Folders/Folder"))
            {
                string temp = node.SelectSingleNode("Name").InnerText;
                if (temp == snode)
                {
                    node.ParentNode.RemoveChild(node);
                }
            }
            xdoc.Save("C:\\product.txt");
        }

Similarly Edit and Reset methods.

Fromm my main program form I call these method using a button click

private void button8_Click(object sender, EventArgs e)
        {
            this.folderBrowserDialog1.ShowNewFolderButton = false;
            this.folderBrowserDialog1.RootFolder = System.Environment.SpecialFolder.MyComputer;
            DialogResult result = this.folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                // user selected folder will be added to the XML Profile for backup
                string path = this.folderBrowserDialog1.SelectedPath;
                XMLProfile xml = new XMLProfile();
                xml.xmlwriter(path);
                listView1.Items.Add(path);
            }
        }

The above method is for adding a new data calling the xmlwriter method from the XMLProfile class.

It is all working fine, but now I realized it is right only to have a Apply button before the changes are finalized and saved.

How do I implement this? I used this link StackOverflow but it did not work for me actually not sure how to implement that in my project. Along with the Apply button I am having a Cancel button. Cancel: Being obvious should not save the changes and just reload the xml file going back to previously saved version.

Any help is appreciated and if any further information is needed please do ask. Thank you.

Community
  • 1
  • 1
Vivian Lobo
  • 583
  • 10
  • 29

2 Answers2

0

You can set your Apply button to be disabled, and enable it only after success on save

evgenyl
  • 7,837
  • 2
  • 27
  • 32
  • Sorry, I might have understood this wrong. You mean that the Apply button is more or less checking if the save is successful. But this would mean a lot more trouble on the Cancel button. – Vivian Lobo Mar 26 '13 at 11:16
  • No, i mean disable apply button, and enable it as last step in Save button code – evgenyl Mar 26 '13 at 12:04
0

Could you also paste your ApplyButtonClick eventhandler here for more precise answer ? But I guess you need something like this

public void OnApplyButtonClick(object sender, EvenArgs e)
{
this.ApplyBtn.Enabled = false;
//Your logic Here
}


public void xmlwriter(string path)
        {
btnApply.Enabled = false;
            XmlDocument xdoc = new XmlDocument();                
            xdoc.Load("C:\\product.txt);
            XmlNode fold = xdoc.CreateElement("Folder");
            XmlNode name = xdoc.CreateElement("Name");
            XmlNode rec = xdoc.CreateElement("Recurse");
            name.InnerText = path;
            rec.InnerText = "true";
            fold.AppendChild(name);
            fold.AppendChild(rec);
            xdoc.SelectSingleNode("//Folders").AppendChild(fold);
            xdoc.Save("C:\\product.txt");
btnApply.Enabled = true;
        }

public void DeleteNode(string snode)
        {
this.ApplyBtn.Enabled = false;
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load("C:\\product.txt");
            foreach (XmlNode node in xdoc.SelectNodes("BackupProfile/Folders/Folder"))
            {
                string temp = node.SelectSingleNode("Name").InnerText;
                if (temp == snode)
                {
                    node.ParentNode.RemoveChild(node);
                }
            }
            xdoc.Save("C:\\product.txt");
this.ApplyBtn.Enabled = true;
    }
Vignesh.N
  • 2,618
  • 2
  • 25
  • 33
  • Actually i have not defined anything yet with the apply button event. `private void button9_Click(object sender, EventArgs e) { //XMLProfile xml = new XMLProfile(); //xml.save(); }` – Vivian Lobo Mar 26 '13 at 14:42
  • Pls try the approach I have mentioned in the answer above, it should work – Vignesh.N Mar 26 '13 at 14:44
  • They are 2 different classes, I am not sure how to do it...as `btnApply.Enabled = false;` is showing error (for me btnApply = button9) – Vivian Lobo Mar 26 '13 at 14:46