C#从数组中保存并重新填充数据

时间:2017-05-25 03:51:02

标签: c# arrays listview save openfiledialog

我正在开发一个C#程序,它将学生姓名,学号,单元号,单元名,标记和出勤存储在一个数组中。所有数据都显示在ListView中。如何从阵列中保存数据,然后使用加载按钮重新填充阵列?提前谢谢。

server_name app.webdomain.ltd webdomain.ltd;

2 个答案:

答案 0 :(得分:0)

在这种情况下,

数组不适用。而是使用列表。

private List<Student> students = new List<Student>();
private void insert(string StudentNumber, string StudentName, string UnitNumber, string UnitName, string Mark, string combobox)
{
   Student s = new Student
   {
     StudentNumber =StudentNumber,
     StudentName =StudentName,
     UnitNumber =UnitNumber
     UnitName =UnitName,
     Mark = Mark
     Combobox = combobox
   };
  students.Add(s);
}


public class Student
{
  public string StudentNumber{get; set;}
  public string StudentName {get; set;}
  public string UnitNumber {get; set;}
  public string UnitName {get; set;}
  public string Mark {get; set;}
  public string Combobox {get;set;}
}

答案 1 :(得分:0)

按照以下步骤

  1. 添加扩展方法以将对象转换为xml
  2. ExtensionMethods类

    public static class ExtensionMethods
    {
        /// <summary>
        /// Converts given class to XML using xml serialization
        /// </summary>
        /// <typeparam name="T">Type of Class</typeparam>
        /// <param name="classObject">Class to be serialized</param>
        /// <returns>Xml string</returns>
        public static string ToXML<T>(this T classObject) where T : class
        {
            XmlSerializer xmls = new XmlSerializer(typeof(T));
            using (MemoryStream ms = new MemoryStream())
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Encoding = new UTF8Encoding(false);
                settings.Indent = true;
                settings.IndentChars = "\t";
                settings.NewLineChars = Environment.NewLine;
                settings.OmitXmlDeclaration = true;
                settings.ConformanceLevel = ConformanceLevel.Document;
                using (XmlWriter writer = XmlTextWriter.Create(ms, settings))
                {
                    xmls.Serialize(writer, classObject);
                }
    
                string xml = Encoding.UTF8.GetString(ms.ToArray());
                return xml;
            }
        }
    
        /// <summary>
        /// Converts given XML string to class of type T
        /// </summary>
        /// <typeparam name="T">Type to be converted</typeparam>
        /// <param name="XmlData">xml string</param>
        /// <returns>class of Type T</returns>
        public static T ToClass<T>(this string XmlData)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            T newClass;
            using (XmlTextReader reader = new XmlTextReader(new StringReader(XmlData)))
            {
                //reader.Namespaces = false;
                newClass = (T)serializer.Deserialize(reader);
            }
            return newClass;
        }
    }
    
    1. 创建一个容纳学生信息的课程
    2. 学生班

      public class Student
      {
        public string StudentNumber{get; set;}
        public string StudentName {get; set;}
        public string UnitNumber {get; set;}
        public string UnitName {get; set;}
        public string Mark {get; set;}
        public string Combobox {get;set;}
      }
      
      1. 在表单加载检查中是否存在文件
      2. 表单加载

            //declare global variable for student list and filepath
            List<Student> students = new List<Student>();
            string FilePath = AppDomain.CurrentDomain.BaseDirectory + "\\" + Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName) + ".xml";
            private void Form1_Load(object sender, EventArgs e)
              {
        
                    string XmlData = string.Empty;
                    if (File.Exists(FilePath))
                    {
                        using (StreamReader sr = new StreamReader(FilePath))
                        {
                            XmlData = sr.ReadToEnd(); 
                        }
                        students = XmlData.ToClass<List<Student>>();
                    }      
              }
        
        1. 在插入,更新和删除操作中将xml文件保存到文件系统
        2. 保存xml

          string XmlData = students.ToXML();
          File.WriteAllText(FilePath, XmlData);