[英]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. 我在序列化对象列表(在我的情况下,类类型为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. 我试图用来将列表序列化为XML文件的代码仅生成具有Class(User)名称的XML文件,而没有其他内容。
{
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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.