[英]XMLDocument.Save adds return carriages to XML when elements are blank
[英]XmlDocument.Save not updating xml file properties
我需要幫助弄清楚doc.save的工作方式。
背景:使用了ac#方法從xml文檔獲取屬性。 然后,我將它們作為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.cs
public void updateSave()
{
foreach (string f in filePaths)
doc.Save(f);
}
該文件看起來像已保存,因為它已將“日期修改:”更新到了我使用保存的那一刻。 我確定有一些參考值混淆,但我無法弄清楚
為何不進行更改?
您不更改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文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.