简体   繁体   中英

C# Serialization of a List XML

I am having an issue serializing a list of objects, in my case, a list of the Class type User.

    [Serializable()]
    public class User 
    {
        string Fname { get; set; }
        string Lname { get; set; }
        string Address { get; set; }
        string City { get; set; }
        string State { get; set; }
        int Zip { get; set; }
        string Phone { get; set; }
        string Email { get; set; }

        public User(string f, string l, string a, string c, string s, int z, string p, string e)
        {
            Fname = f;
            Lname = l;
            Address = a;
            City = c;
            State = s;
            Zip = z;
            Phone = p;
            Email = e;
        }

        public User() { }
    }

The code I am trying to use to serialize the list to an XML file only produces an XML file with the name of the Class(User) and nothing else.

{
    string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    string fullpath = System.IO.Path.Combine(path, "Users.txt");
    Stream stream = File.OpenWrite(fullpath);
    XmlSerializer writer = new XmlSerializer(typeof(List<User>));

    writer.Serialize(stream, Users);
}

What exactly am I doing wrong here. Just to be clear, this is for homework. I am at a loss as to what to change to get the data stored into the file. Thanks.

try this

    [Serializable()]
    public class User 
    {
        public string Fname { get; set; }
        public string Lname { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public int Zip { get; set; }
        public string Phone { get; set; }
        public string Email { get; set; }

        public User(string f, string l, string a, string c, string s, int z, string p, string e)
        {
            Fname = f;
            Lname = l;
            Address = a;
            City = c;
            State = s;
            Zip = z;
            Phone = p;
            Email = e;
        }

        public User() { }
    }

To be able to serialize / deserialize a class, the serializer requires a parameterless constructor. So, you need to add the parameterless constructors only to your classes

    public class User
    {
        public string Fname { get; set; }
        public string Lname { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public int Zip { get; set; }
        public string Phone { get; set; }
        public string Email { get; set; }

        // parameterless constructor. There is no need to declare it. 
        public User() { }
        static void Main(string[] args)
        {
            User n = new User();
            n.Fname = "fname";
            n.Lname = "lname";
            n.Address = "address";
            n.City = "city";
            n.State = "state";
            n.Zip = 1;
            n.Phone = "phone";
            n.Email = "email";
            SaveXML.SaveData(n, "xml.xml");
        }
        class SaveXML
        {
            public static void SaveData(object obj, string filename)
            {
                // initialization of XML serializer.
                XmlSerializer sr = new XmlSerializer(obj.GetType());
                // get stream from string
                TextWriter writer = new StreamWriter(filename);
                // Serialization 
                sr.Serialize(writer, obj);
                writer.Close();
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM