C#XmlSerializer serializer.Serialize

时间:2016-10-28 01:14:48

标签: c# serialization xml-serialization deserialization xmlserializer

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

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

(field)Account [] 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)
    {
    }
}

}

1 个答案:

答案 0 :(得分:0)

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