如何创建按钮以将texboxes值保存为XML并使用按钮将其加载回来? C# [英] How do I create button to save texboxes value into XML and load it back with buttons? C#

查看:77
本文介绍了如何创建按钮以将texboxes值保存为XML并使用按钮将其加载回来? C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我真的很新,对c和#抱歉我的英语......我即将为学校项目申请,即时创建一个简单的现金注册应用程序..



所以我想创建一个按钮来保存我创建的texbox中的每个文本/数字值

程序运行但是,我点击保存按钮后得到这个错误我已创建

first of all im really new to this and new with c# sorry about my english..well im about to make an application for school project, im creating a simple cashregister app thing..

so i wanted to make a button to save every text/number value in texboxes i've created
the program runs but, im getting this error after click the save button i've created

An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll
Absolute path information is required. occurred



此行


at this line

FileIOPermission filePermission =
                    new FileIOPermission(FileIOPermissionAccess.AllAccess, "data.xml");





我的尝试:



form1.cs

保存按钮



What I have tried:

form1.cs
save button

private void btnsave_Click_1(object sender, EventArgs e)
       {
           if (File.Exists("data.xml"))
           {
               File.SetAttributes("data.xml", FileAttributes.Normal);
               FileIOPermission filePermission =
                   new FileIOPermission(FileIOPermissionAccess.AllAccess, "data.xml");

               using (FileStream stream = new FileStream("data.xml", FileMode.Create))
               {
                   try
                   {
                       Information info = new Information();
                       info.Data1 = txtBasoSU.Text;
                       info.Data2 = txtBasoC.Text;
                       info.Data3 = txtBasoM.Text;
                       info.Data4 = txtBasoB.Text;
                       info.Data5 = txtMieA.Text;
                       info.Data6 = txtMieAB.Text;
                       info.Data7 = txtServisMinuman.Text;
                       info.Data8 = txtServisMakanan.Text;
                       info.Data9 = txtServisBungkus.Text;
                       info.Data10 = txtTotalSpajak.Text;
                       info.Data11 = txtTotalPajak.Text;
                       info.Data12 = txtTotal.Text;

                       SaveXML.SaveData(info, "data.xml");

                   }
                   catch (Exception ex)
                   {
                       MessageBox.Show(ex.Message);
                   }
               }
           }
       }



加载按钮


load button

private void btnload_Click(object sender, EventArgs e)
        {
            if (File.Exists("data.xml"))
            {
                using (var stream = new FileStream("data.xml", FileMode.Open))
                {
                    var xs = new XmlSerializer(typeof(Information));
                    FileStream read = new FileStream("data.xml", FileMode.Open, FileAccess.ReadWrite);
                    Information info = (Information)xs.Deserialize(read);
                    // FileStream read = new FileStream("data.xml", FileMode.Open, FileAccess.ReadWrite);
                    //Information info = (Information)xs.Deserialize(read);

                    txtBasoSU.Text = info.Data1;
                    txtBasoC.Text = info.Data2;
                    txtBasoM.Text = info.Data3;
                    txtBasoB.Text = info.Data4;
                    txtMieA.Text = info.Data5;
                    txtMieAB.Text = info.Data6;

                    txtServisMinuman.Text = info.Data7;
                    txtServisMakanan.Text = info.Data8;
                    txtServisBungkus.Text = info.Data9;
                    txtTotalSpajak.Text = info.Data10;
                    txtTotalPajak.Text = info.Data11;
                    txtTotal.Text = info.Data12;
                }
            }
        }





information.cs



information.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace CS_Sistem_Managemen_Restoran
{
    public class Information
    {
        private string data1;
        private string data2;
        private string data3;
        private string data4;
        private string data5;
        private string data6;

        private string data7;
        private string data8;
        private string data9;
        private string data10;
        private string data11;
        private string data12;
        
        
        public string Data1
        {
            get { return data1; }
            set { data1 = value; }
        }

        public string Data2
        {
            get { return data2; }
            set { data2 = value; }
        }
        public string Data3
        {
            get { return data3; }
            set { data3 = value; }
        }

        public string Data4
        {
            get { return data4; }
            set { data4 = value; }
        }

        public string Data5
        {
            get { return data5; }
            set { data5 = value; }
        }

        public string Data6
        {
            get { return data6; }
            set { data6 = value; }
        }

        public string Data7
        {
            get { return data7; }
            set { data7 = value; }
        }

        public string Data8
        {
            get { return data8; }
            set { data8 = value; }
        }

        public string Data9
        {
            get { return data9; }
            set { data9 = value; }
        }

        public string Data10
        {
            get { return data10; }
            set { data10 = value; }
        }

        public string Data11
        {
            get { return data11; }
            set { data11 = value; }
        }

        public string Data12
        {
            get { return data12; }
            set { data12 = value; }
        }

        
    }
}





saveXML.cs



saveXML.cs

using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.IO;

namespace CS_Sistem_Managemen_Restoran
{
    public class SaveXML
    {
        public static void SaveData(object obj, string filename)
        {
            XmlSerializer sr = new XmlSerializer(obj.GetType());
            TextWriter writer = new StreamWriter(filename);
            sr.Serialize(writer, obj);
            writer.Close();
        }
    }
}

推荐答案

如果你看一下这个错误是非常自我解释的它。

The error is pretty self explanatory, if you look at it.
Absolute path information is required



来这一行:


Is coming on this line:

FileIOPermission filePermission =
                    new FileIOPermission(FileIOPermissionAccess.AllAccess, "data.xml");

因为data.xml是 相对路径 - 它与当前应用程序在此精确时刻运行的位置有关。 DLL不知道它在哪里,或者至少不确定它是否知道,因此它在您指定文件名时专门测试绝对位置。



这有点像在图书馆里,和电话里的某个人说话。当你告诉他们你正在阅读这本书并没有帮助他们来看看它,因为这本书是一个相对的地址:你需要说:3楼,IT部门,第14行,第3层, OOP编程的基础知识为他们提供了一个绝对的地址,让他们走上楼梯找到它。



所以用它代替data.xml绝对地址:@C:\我把东西放在这里\ XML文件\ datata.xml,它应该摆脱错误。



由看看这个:我应该在哪里存储我的数据? [ ^ ] - 它可能对您来说有点先进,但它提示了一些保存数据的合理位置,并向您展示如何获取它们的路径。

Because "data.xml" is a relative path - and it's relative to wherever the current app is running at this precise moment. The DLL doesn't know where that is, or at the very least isn't sure that it knows, so it specifically tests for an absolute location when you specify the file name.

It's a bit like being in a library, and talking to someone on the phone. When you tell them you are reading "this book" that doesn't help them come and look at it because "this book" is a relative address: you need to say: "Floor 3, IT Section, Row 14, Shelf 3, 'Fundamentals of OOPs programming'" to provide them with an absolute address for them to walk up the stairs and find it.

So replace "data.xml" with it's absolute address: @"C:\I keep stuff here\XML files\data.xml" and it should get rid of the error.

By the way, have a look at this: Where should I store my data?[^] - it may be a little advanced for you, but it suggests some sensible places to keep data, and shows you how to get the path to them.


这篇关于如何创建按钮以将texboxes值保存为XML并使用按钮将其加载回来? C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆