I'm new to the XmlSerializer. I've written a small class to hold entries from a database:
[Serializable]
public struct Entry
{
public string artkey, lid, request, status, requestdate;
}
Simple enough, right? It should be a piece of cake to serialize a list of these.
I have a function that compiles a list of these. To serialize my list, I try the following code:
XmlSerializer serializer = new XmlSerializer(typeof(Entry));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
serializer.Serialize(ms, entries.ToArray());
ms.WriteTo(Response.OutputStream);
This code prints the following exception:
<error>System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidCastException: Specified cast is not valid.
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterEntry.Write3_Entry(Object o)
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
at System.Xml.Serialization.XmlSerializer.Serialize(Stream stream, Object o, XmlSerializerNamespaces namespaces)
at System.Xml.Serialization.XmlSerializer.Serialize(Stream stream, Object o)
at CCB_Requests.xmlResponse_selectFromCcb_Requests(HttpResponse response)
at CCB_Requests.ProcessRequest(HttpContext context)</error>
It seems that I must be making a simple mistake. How can I fix this?
structin c# doesnt mean "basic object"; it has a very specific meaning, that isn't this. Mutable structs are notorious for causing odd data issues, with people not quite anticipating their copy semantics. It is also oversized; structs are efficient for small values; this has multiple references. Public fields break all rules of encapsulation and abstraction. Need I go on!