【问题标题】:saving and loading files from a object array list [closed]从对象数组列表中保存和加载文件[关闭]
【发布时间】:2015-07-27 02:23:19
【问题描述】:

您好,我正在尝试使用对象数组来构建联系人管理器程序来存储数据。我需要查看显示可用联系人摘要的报告,然后有一个菜单允许用户与程序进行交互。我需要有保存和加载文件的选项,但我不确定如何执行此操作。

任何指导将不胜感激。

static void Main(string[] args)
        {
            //The MAXPLAYERS constant is the physical table size
            const Int32 MAXCONTACTS = 23;

            //Declare the player tables

            Contact[] contacts = new Contact[MAXCONTACTS];

            //Keep track of the actual number of players (i.e. logical table size)
            Int32 contactCount = 0;
            Int32 number = 0;
            String lastName = " ";
            String phoneNumber = " "; 
            String emailAddress = " "; 

            String firstName = " "; ;

            Console.WriteLine("Contact List");
            // display the menu to the user
            Console.WriteLine("Enter option or M for menu:");
            //Main Driver
            char menuItem;
            Console.WriteLine("Welcome to the player system...\n");
            menuItem = GetMenuItem();
            while (menuItem != 'X')
            {

                ProcessMenuItem(menuItem, number, firstName, lastName, phoneNumber, emailAddress, contacts, ref contactCount, MAXCONTACTS);
                menuItem = GetMenuItem();

            }
            Console.WriteLine("\nThank you, goodbye");
            Console.ReadLine();
        }
        static char GetMenuItem()
        {
            char menuItem;
            DisplayMenu();
            menuItem = IOConsole.GetChar((Console.ReadLine()));

            while (menuItem != 'C'
                && menuItem != 'L' && menuItem != 'X' && menuItem != 'R' && menuItem != 'U' && menuItem != 'D')
            {
                Console.WriteLine("\nError - Invalid menu item");
                DisplayMenu();
                //menuItem = IOConsole.GetChar((Console.ReadLine()));
            }
            return menuItem;
        }

        static void DisplayMenu()
        {
           Console.WriteLine("C-> Create Contacts");
           Console.WriteLine("R-> Remove Contacts");
           Console.WriteLine("U-> Update Contacts");
           Console.WriteLine("D -> Load data from file");
           Console.WriteLine("S-> Save data to file");
           Console.WriteLine("L-> View sorted by last name");
           Console.WriteLine("F-> View sorted by first name");
           Console.WriteLine("P-> View by partial name search");
           Console.WriteLine("T-> View by contact type");
           Console.WriteLine("Q-> Quit");
        }

        //Routes to the appropriate process routine based on the user menu choice
        static void ProcessMenuItem(Char menuItem, Int32 number, String firstName, String lastName, String phoneNumber,
            String emailAddress, Contact[] contacts, ref Int32 contactCount, Int32 MAXCONTACTS)
        {
            switch (menuItem)
            {
                case 'C':
                    createContact();
                    break;
                case 'R':
                    removeContact();
                    break;
                case 'U':
                    updateContact();
                    break;
                case 'D':
                    LoadToFile();
                    break;
                case 'S':
                    saveToFile();
                    break;

                case 'L':
                    sortByLastName();
                    break;
                case 'F':
                    sortByFirstName();
                       break;
                case 'P':

                       break;
                case 'T':

                       break;
                case 'Q':

                       break;

            }                   
        }


        public static void saveToFile()
        {

        }
        public static void LoadToFile()
        {

        }

【问题讨论】:

  • 你的问题太模糊了。如果您问我们应该如何将数据存储在文件中,那么您问错了地方,因为这不是本网站的用途。如果您已经尝试将数据存储在文件中但未成功,那么本网站可让您发布您的代码和具体错误的描述。

标签: c#


【解决方案1】:

您有几种可能的方法来保存/加载您的 Contact 对象数组:

  1. 序列化 (Serialize ArrayList of Objects)

  2. XML (https://msdn.microsoft.com/en-us/library/ms172872.aspx)

  3. JSON (https://msdn.microsoft.com/en-us/library/bb412179(v=vs.110).aspx)

...或您想要的任何其他格式。

但是您可以专注于上面的那些格式,看看哪种方法最适合您的情况。

作为附加解决方案,您可以尝试学习此代码,使用DataSetDataTable 保存您的Contact 对象,并将其写入xml 文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace TestContacts
{
    class Contact
    {
        public string Name = "";
    }

    class Program
    {
        static void Main(string[] args)
        {
            Contact c1 = new Contact();
            c1.Name = "Name1";

            Contact c2 = new Contact();
            c2.Name = "Name2";

            Contact c3 = new Contact();
            c3.Name = "Name3";

            //Simpliest approach could be saving it to data table 
            DataSet ds = new DataSet("Contact");
            DataTable dt = new DataTable("Info");
            dt.Columns.Add("name", typeof(string));

            //You can loop but for simplicity add one by one
            DataRow r1 = dt.NewRow();
            r1["name"] = c1.Name;
            dt.Rows.Add(r1);

            DataRow r2 = dt.NewRow();
            r2["name"] = c2.Name;
            dt.Rows.Add(r2);

            DataRow r3 = dt.NewRow();
            r3["name"] = c3.Name;
            dt.Rows.Add(r3);

            //Save to file using XML file format
            ds.Tables.Add(dt);
            ds.WriteXml("C:\\contacts.xml", XmlWriteMode.WriteSchema);

            //You can also load via
            ds.ReadXml("C:\\contacts.xml", XmlReadMode.ReadSchema);

        }
    }
}

【讨论】:

    猜你喜欢
    • 2019-06-13
    • 2011-01-24
    • 2021-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 2016-02-21
    • 1970-01-01
    相关资源
    最近更新 更多