Question

I need help figuring out how doc.save works.

Background: got a c# method that gets properties from an xml document. I then sent these as the dataset for a DataGridView in windows form. Im trying to make it so that when the user edits the form the xml values get updated.

First I parse the XML: Updater.cs

XmlNodeList elemList = doc.GetElementsByTagName("property");
for (int i = 0; i < elemList.Count; i++)
{
    if (elemList[i].Attributes["value"] != null)
    {
        AppProperty property = new AppProperty(elemList[i].Attributes["name"].Value, elemList[i].Attributes["value"].Value);
        properties.Add(property);
    }
}

Then I send it to the form and update the form dataset: Form1.cs

private void Form1_Load(object sender, System.EventArgs e)
{
    this.dataGridView1.SelectionMode =
    DataGridViewSelectionMode.FullRowSelect;
    this.dataGridView1.DataSource = properties;
    this.dataGridView1.AutoGenerateColumns = false;
}

Now when the user edits I trigger an event listener: Form.cs

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    updater.updateSave();
}

This then goes back to my updater class and saves the document:Updater.cs

public void updateSave()
{
    foreach (string f in filePaths)
            doc.Save(f);
}

The file looks like it was saved since it has updated the "Date Modified: " to the moment i used the save. I'm sure there is some reference-value mix up but I cannot figure it out

How come the changes are not being made?

Was it helpful?

Solution

You're not changing the XML document, you're changing a copy of some attributes

if (elemList[i].Attributes["value"] != null)
{
     //You're making a copy of the attribute's value here:
     AppProperty property = new AppProperty(elemList[i].Attributes["name"].Value, elemList[i].Attributes["value"].Value);
     properties.Add(property);
}

The GridView changes the properties dataset, and these changes aren't propagated back to the XML document.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top