c# XmlSerializer与serializer.Serialize的问题

本文关键字:问题 Serialize serializer XmlSerializer | 更新日期: 2023-09-27 18:15:23

长话短说,我正在为学校的作业申请ATM。银行账户信息需要存储到一个文件中,以使这些账户余额保持最新状态。

我有两个问题-(1)在序列化过程中,我在第49行得到一个错误,说:

(字段)帐户[]RunAccount.acctArray

非静态字段、方法或属性"RunAccount.acctArray"需要对象引用

(2)我的读入和读出序列化位置有意义吗?

我在这方面很新,感觉我不知道我在做什么,所以所有的建议都是感激和欢迎的。谢谢!

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml.Serialization;
    namespace Bank_Midterm_Project
    {
        public class RunAccount
         {
    Account[] acctArray = new Account[3];
    private static int i;
    bool acctscreated = false;
    bool acctsloaded = false;
    private object test;
    public static void Main(String[] args)
    {
        RunAccount ra = new RunAccount();
        int input;
        do
        {
            Console.WriteLine("Please enter a choice:");
            Console.WriteLine("1) Populate Accounts");
            Console.WriteLine("2) Load Accounts From File");
            Console.WriteLine("3) Select Account");
            Console.WriteLine("4) Exit");
            input = Convert.ToInt32(Console.ReadLine());
            if (input == 1 && ra.acctscreated == false)
            {
                ra.populateArray();
            }
            else if (input == 2 && ra.acctsloaded == false)
            {
                ra.readArray();
            }
            else if (input == 3 && ra.acctscreated == true)
            {
                ra.pickAccountMenu();
            }
            else if (input == 4)
            {
              Stream FileStream = File.Create("test.xml");
              XmlSerializer serializer = new XMLSerializer(typeof(Account[]));
              serializer.Serialize(FileStream, acctArray);
              FileStream.Close();
            }
            else
            {
                if (input == 1 && ra.acctscreated == true)
                {
                    Console.WriteLine("You have already populated the accounts.  Please try again.");
                }
                else if (input == 2 && ra.acctscreated == true)
                {
                    Console.WriteLine("You have already loaded the accounts.  Please try again.");
                }
                else if (input == 3 && ra.acctscreated == false)
                {
                    Console.WriteLine("You must create the accounts first.  Please try again.");
                }
            }

        } while (input != 5);
            //ATM atm = new ATM();
            //atm.topMenu();

    //ra.writeArray();
    //{
    //}
}
    //private void readArray()
    //{
    //    throw new NotImplementedException();
    public void readArray()
    {
        Stream FileStream = File.OpenRead("test.xml");
        XmlSerializer deserializer = new XmlSerializer(typeof(Account[]));
        acctArray = (Account[])deserializer.Deserialize(FileStream);
        FileStream.Close();
    }
//}

    public void populateArray()
    {
        //int[] acctArray = new int[3];
        //prompt for username
        Console.WriteLine("Please enter three account numbers, separated by spaces:");
        string[] tokens = Console.ReadLine().Split();
        for (int i = 0; i < acctArray.Length; i++)
        {
            acctArray[i] = new Account(tokens[i]);
        }
        acctscreated = true;
    }
    public void pickAccountMenu()
    {
        string sinput = null;
        int input = -1;
        while (input != 4)
        {
            Console.WriteLine("Please enter 0, 1, or 2 for your account.  4 to quit.");
            sinput = Console.ReadLine();
            input = Convert.ToInt32(sinput);
            if (input != -99)
            {
                acctArray[input].menu();
            }
        }
    }
}
internal class XMLSerializer : XmlSerializer
{
    public XMLSerializer(Type type) : base(type)
    {
    }
}

}

c# XmlSerializer与serializer.Serialize的问题

使用ra.acctArray。您必须使用类实例变量访问该属性。