开发者

is it possible to store structure in Text file using c#

i lik开发者_如何学编程e store the structure in to the Text file and also retrive the same.

i created a structure name student details consists the values

studentid,student name,student avg.i like to store this structure details in Textfile for many students.after that i like to reteive that details from Textfile using student id.i am working in wondows form application.is it possible in c#.


You could store your collection of structures in an XML file, which is structured text.
Here is a very quick example to get you started:

    using System.Collections.Generic;
    using System.IO;
    using System.Xml;
    using System.Xml.Serialization;

    public class Student {
        [XmlElement("id")]
        public int id;
        [XmlElement("name")]
        public string name;
    }

    [XmlRoot("students")]
    public class Students {
        [XmlElement("students")]
        public List<Student> students = new List<Student>();
    }

    class Program {
        static void Main(string[] args) {
            Students s = new Students();
            s.students.Add(new Student() { id = 1, name = "John Doe" });
            s.students.Add(new Student() { id = 2, name = "Jane Doe" });
            XmlSerializer xs = new XmlSerializer(typeof(Students));
            using (FileStream fs = new FileStream("students.xml", FileMode.Create, FileAccess.Write)) {
                xs.Serialize(fs, s);
            }
        }
    }

You will get something like:

<?xml version="1.0"?>
<students xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <students>
    <id>1</id>
    <name>John Doe</name>
  </students>
  <students>
    <id>2</id>
    <name>Jane Doe</name>
  </students>
</students>


You could also take a look at XmlSerializer:

Serializes and deserializes objects into and from XML documents. The XmlSerializer enables you to control how objects are encoded into XML.


Well, there's XML


You could use an xml file; xml is essentially structured text, which seems to be exactly what you are looking for. Start with the System.Xml namespace.


Like Paolo (and the others) i would create a specific class and a container class like these:

[DataContract]
public class Student
{
    [DataMember]
    public int ID { get; set; }

    [DataMember]
    public string FirstName { get; set; }

    [DataMember]
    public string LastName { get; set; }
}

[DataContract]
public class StudentList
{
    [DataMember]
    public string Name { get; private set; }

    [DataMember]
    public List<Student> Students { get; private set; }

    public StudentList(string name)
    {
        Name = name;
        Students = new List<Student>();
    }
}

These can than be (de)serialized by a DataContractSerializer. The above classes are just a rough starting point. Depending on your needs you have to think about private setters, immutability, etc.

But if you have such specific classes you can easily create a BindingSource, put your StudentList.Students into it and give this BindingSource into a DataGridView to visualize and change the list.


You just need to Serialize Objects to a File. Check the article.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜