保存文件后,Datagrid不会显示解密的XML数据...我是否需要关闭该编写器?

问题描述 投票:0回答:2

我正在进行数据解密,我正在使用数据网格来查看数据。编辑并保存现有记录后,会出现此问题。在保存过程之后,数据被解密,但是如果点击任何数据行,则数据突然变为加密。编写者是否对此负责?我需要关闭编写器吗?

namespace someProgram
{
    public partial class formManage : Form
    {
        private readonly XmlSerializer xs;
        private AddressBook ls;
        private int _counter = 0;
        private string currentFileName;
        public string title { get; set; }
        int selectedRow;
        private static Random random = new Random();

        public formManage()
        {
            InitializeComponent();

            ls = new AddressBook();
            xs = new XmlSerializer(typeof(AddressBook));
            currentFileName = "";
        }

        public void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var saveFileDialog = new SaveFileDialog();
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.Title = "Select save location file name";
            saveFileDialog.Filter = "XML-File | *.xml";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                StreamWriter writer = new StreamWriter(saveFileDialog.FileName)

                    SaveFile(writer);

            }
        }

        public void SaveFile(StreamWriter writer)
        {
            foreach (var item in ls.Contacts)
            {
                item.Question1 = XMLEncryption.Model.Helpers.Encryptor.Encrypt(item.Question1);
                item.Question2 = XMLEncryption.Model.Helpers.Encryptor.Encrypt(item.Question2);
                item.Question3 = XMLEncryption.Model.Helpers.Encryptor.Encrypt(item.Question3);
            }
            xs.Serialize(writer, ls);
            MessageBox.Show("File saved... ");
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            selectedRow = e.RowIndex;
            if (selectedRow >= 0)
            {
                buttonUpdate.Enabled = true;
                DataGridViewRow row = dataGridView1.Rows[selectedRow];

                if (dataGridView1.SelectedRows.Count > 0)
                {
                    string question1 = dataGridView1.SelectedRows[0].Cells[4].Value + string.Empty;
                    string question2 = dataGridView1.SelectedRows[0].Cells[6].Value + string.Empty;
                    string question3 = dataGridView1.SelectedRows[0].Cells[8].Value + string.Empty;

                    textBoxQuestion1.Text = question1;
                    textBoxQuestion2.Text = question2;
                    textBoxQuestion3.Text = question3;
                }
            }
        }
    }
}
c# xml serialization datagridview deserialization
2个回答
1
投票

您是否需要加密内存中使用的地址簿问题?如果不是,您可以创建一个在保存时加密的地址簿的新实例。

public void SaveFile(StreamWriter writer)
{
    AddressBook encryptedBook = New AddressBook();

    foreach (var item in ls.Contacts)
    {
        var encryptedContact = new Contact
        {
            Question1 = XMLEncryption.Model.Helpers.Encryptor.Encrypt(item.Question1),
            Question2 = XMLEncryption.Model.Helpers.Encryptor.Encrypt(item.Question2),
            Question3 = XMLEncryption.Model.Helpers.Encryptor.Encrypt(item.Question3)
        };

        encryptedBook.Contacts.Add(encryptedContact);
    }

    xs.Serialize(writer, encryptedBook);
    MessageBox.Show("File saved... ");
}

0
投票

我找到了答案。对于实现iDispose的对象,如streamreader和streamwriter ......应该使用“Using”语句,它将自动处理流。对于前...

using(StreamReader sr = new StreamReader("myfile.txt")){//do stuff here}

这确保了流式读取器的处理,我可以通过重新加载页面或调用打开的例程来重新加载文档。基本上我在任何地方使用流读取或写入类,我将它们包装在using语句中,并且因为流被正确处理而解决了错误。

© www.soinside.com 2019 - 2024. All rights reserved.