繁体   English   中英

从XML文件中删除和读取

[英]Removing and reading from XML file

每当我尝试使用按钮单击从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;
    }
}

}

StaffCreate_FormClosing你填充xDocument与文件(的现有内容xDocument.Load ),然后你在一切都在增加内存for循环,其中包含无论是文件,当它被加载(减去重复删除的项目加上加项目)。 结果,所有未被删除的内容都会被添加两次。 如果您将所有内容存储在内存中,则无需从文件中加载文件-您不需要任何文件。

在StaffCreate_FormClosing方法中,加载文件并为当前内存中的所有人员实体附加新节点。 在追加之前从人员节点(xnode)删除所有子代应该可以解决问题。

您应该每次都清除xml文件的内容,或者跟踪那些nodes -在您的代码中,这些人员对象的状态:是最近创建的,还是从现有xml文件加载的。 也许像一个简单的“ ORM”东西。

并且,也许使用Path.Combine()比手动进行路径字符串组合更好

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM