每当我尝试使用按钮从xml文件中删除条目时,单击它会将其从表单中删除,但它仍保留在xml文件中。我遇到的另一个问题是:
我在程序中创建了一个用户,它保存到xml文件中。我关闭程序并将其加载回第一次我将条目放入其中后将其重新加载,然后正确读取并在xml文件中显示了什么。但是,如果我一次又一次地关闭并打开程序,每次打开它时都会反复复制xml文件中的条目。
有什么建议吗?
public partial class StaffCreate : Form
{
public StaffCreate()
{
InitializeComponent();
}
List<Person> people = new List<Person>();
private void StaffCreate_Load(object sender, EventArgs e)
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
if (!Directory.Exists(path + "\\stockbox Documents"))
Directory.CreateDirectory(path + "\\stockbox Documents");
if (!File.Exists(path + "\\stockbox Documents\\People_File.xml"))
{
XmlTextWriter xWriter = new XmlTextWriter(path + "\\stockbox Documents\\People_File.xml", Encoding.UTF8);
xWriter.WriteStartElement("People");
xWriter.WriteEndElement();
xWriter.Close();
}
XmlDocument xDoc = new XmlDocument();
xDoc.Load(path + "\\stockbox Documents\\People_File.xml");
foreach (XmlNode xNode in xDoc.SelectNodes("People/Person"))
{
Person p = new Person();
p.name = xNode.SelectSingleNode("Name").InnerText;
p.pass = xNode.SelectSingleNode("Password").InnerText;
p.title = xNode.SelectSingleNode("Title").InnerText;
people.Add(p);
listView1.Items.Add(p.name);
}
}
private void button2_Click(object sender, EventArgs e)
{
Person p = new Person();
p.name = nameTxt.Text;
p.pass = passTxt.Text;
p.title = titleTxt.Text;
people.Add(p);
listView1.Items.Add(p.name);
nameTxt.Text = "";
passTxt.Text = "";
titleTxt.Text = "";
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
nameTxt.Text = people[listView1.SelectedItems[0].Index].name;
passTxt.Text = people[listView1.SelectedItems[0].Index].pass;
titleTxt.Text = people[listView1.SelectedItems[0].Index].title;
}
}
private void button3_Click(object sender, EventArgs e)
{
remove();
nameTxt.Text = "";
passTxt.Text = "";
titleTxt.Text = "";
}
void remove()
{
try
{
people.RemoveAt(listView1.SelectedItems[0].Index);
listView1.Items.Remove(listView1.Items[0]);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
people[listView1.SelectedItems[0].Index].name = nameTxt.Text;
people[listView1.SelectedItems[0].Index].pass = passTxt.Text;
people[listView1.SelectedItems[0].Index].title = titleTxt.Text;
listView1.SelectedItems[0].Text = nameTxt.Text;
}
private void StaffCreate_FormClosing(object sender, FormClosingEventArgs e)
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
XmlDocument xDocument = new XmlDocument();
xDocument.Load(path + "\\stockbox Documents\\People_File.xml");
XmlNode xNode = xDocument.SelectSingleNode("People");
foreach (Person p in people)
{
XmlNode xTnode = xDocument.CreateElement("Person");
XmlNode xName = xDocument.CreateElement("Name");
XmlNode xPass = xDocument.CreateElement("Password");
XmlNode xTitle = xDocument.CreateElement("Title");
xName.InnerText = p.name;
xPass.InnerText = p.pass;
xTitle.InnerText = p.title;
xTnode.AppendChild(xName);
xTnode.AppendChild(xPass);
xTnode.AppendChild(xTitle);
xDocument.DocumentElement.AppendChild(xTnode);
}
xDocument.Save(path + "\\stockbox Documents\\People_File.xml");
}
}
class Person
{
public string name
{
get;
set;
}
public string pass
{
get;
set;
}
public string title
{
get;
set;
}
}
}
答案 0 :(得分:0)
在StaffCreate_FormClosing
中,您使用文件的现有内容(xDocument
)填充xDocument.Load
,然后在for
循环中添加内存中的所有内容,其中包含加载时文件中的内容(重复删除已删除的项目和添加的项目)。因此,任何未被删除的内容都会被添加两次。如果您拥有内存中的所有内容,则无需从文件中加载 - 您不需要其中任何内容。
答案 1 :(得分:0)
在StaffCreate_FormClosing方法中,加载文件并为当前在内存中的所有人员实体添加新节点。在追加之前从人员节点(xnode)中删除所有子节点应该可以解决问题。
答案 2 :(得分:0)
您应该每次都清除xml文件内容,或者跟踪那些nodes
- 代码中的那些人对象的状态:它们是最近创建的,还是从现有的xml文件加载的。也许就像一个简单的“ORM”事。
并且,使用Path.Combine()
可能比手动执行路径字符串组合更好