2013-11-20 47 views
0

我需要帮助了解如何doc.save工作。XmlDocument.Save不更新xml文件属性

背景:获得了一个从xml文档获取属性的c#方法。然后,我将这些作为Windows窗体中DataGridView的数据集发送。我试图做到这一点,当用户编辑表单的XML值得到更新。

首先我解析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); 
    } 
} 

然后我把它发送到形式和更新表单数据集: Form1.cs中

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

现在,当用户编辑我触发事件监听器:Form.cs

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

然后返回到我的updater cla ss和保存文件:Updater.cs

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

文件看起来像保存它,因为它已经更新了“修改日期:”到我所使用的保存的时刻。我敢肯定,有一些参考价值混淆,但我不知道它

这些变化怎么没有被作出?

回答

1

你不改变XML文档,你改变一个拷贝一些属性

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

GridView控件改变properties数据集,而这些变化不会传播回XML文件。

+0

请注意,它们不会传播,因为它们根本与XmlDocument无关。 –

+0

所以会使用elemList [i] .Attributes [“名称”]引用xml文档? – Commanderson

+0

是的。您可以将此引用存储在AppProperty对象中,并在保存文档之前对其进行更新。 – Reda