Reputation: 65
When I'm trying to serialize my list of customers i get file
<?xml version="1.0"?>
<Customers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
No exception thrown. I'm pretty sure it was already working, but i probably changed something somewhere and now I have no idea why it's not working anymore.
I have class CustomerList
[XmlRoot("Customers")]
[XmlInclude(typeof(Customer))]
public class CustomerList
{
[XmlArray("CustomerList")]
[XmlArrayItem("Customer")]
private List<Customer> Customers = new List<Customer>();
private int position = -1;
...;
}
which does contain items of customer class
[XmlType("Customer")]
public class Customer
{
private string name = string.Empty;
private string surname = string.Empty;
private string phone = string.Empty;
private string email = string.Empty;
[XmlElement("Name")]
public string Name
{
get { return name; }
set { name = value; }
}
[XmlElement("Surname")]
public string Surname
{
get { return surname; }
set { surname = value; }
}
[XmlElement("Phone")]
public string Phone
{
get { return phone; }
set { phone = value; }
}
[XmlElement("Email")]
public string Email
{
get { return email; }
set { email= value; }
}
...;
}
And this is serialized with
private static Type[] extra = { typeof(Customer) };
private XmlSerializer serializer = new XmlSerializer(typeof(CustomerList), extra);
public void Serialize(CustomerList Customers)
{
System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Create);
serializer.Serialize(fs, Customers);
fs.Close();
}
Upvotes: 0
Views: 807
Reputation: 1500155
Your variables are all private. XmlSerializer
can only serialize public properties and fields. From the documentation:
XML serialization is the process of converting an object's public properties and fields to a serial format (in this case, XML) for storage or transport.
and later:
To control the generated XML, you can apply special attributes to classes and members. For example, to specify a different XML element name, apply an
XmlElementAttribute
to a public field or property, and set theElementName
property.
I suggest that you convert your private fields into public properties (not public fields).
Upvotes: 2