4

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?

5
  • 3
    Btw, public fields: bad; mutable structs: bad; structs for entities: bad... This should be a class with properties Commented Mar 30, 2011 at 16:05
  • Oh, and [Serializable] doesn't impact XML serialization. But the bottom closing brace is fine :) Commented Mar 30, 2011 at 16:11
  • How would this be better if this were a class with properties? Commented Mar 30, 2011 at 16:25
  • struct in 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! Commented Mar 30, 2011 at 17:30
  • to turn that question around; if you can't clearly articulate why it is a struct, then default to class. It is a common mistake in c# to use a stuct because a type is "simple", but that is very very far from the real use of struct, and frankly I've had to answer far to many "why doesn't this work like I expect?" "because that really shouldn't be a struct". Honestly, that should be a class. Commented Mar 30, 2011 at 17:35

3 Answers 3

5

you are serializing an array of Entry, change the initialization of the XmlSerializer to:

// typeof(Entry) ==> typeof(Entry[])
XmlSerializer serializer = new XmlSerializer(typeof(Entry[]));
Sign up to request clarification or add additional context in comments.

2 Comments

Your answer got rid of the exception, but the result is simply a collage of all the database entries with no XML markup. Do I need to put some attributes on my class to make it work?
Oh, scratch that last comment. I just don't see the XML markup because I'm viewing it in a browser that interprets it as HTML!
1

Writing this as wiki, as it doesn't answer the question, but show how this type should be written:

public class Entry
{
    [XmlElement("artKey")]
    public string ArtKey {get;set;}

    // etc
}

for reasons, see the comments I added to the qestion

Comments

0

Dont use typeof() sometime is your Entry is Null or in Faulted State then it shows InvalidCastException so apart from that use GetType() method which will gives you same output as by typeof().

   Entry e = new Entry();
            e.artkey = "as";
            e.lid = "lid";
            e.request = "request";
            e.requestdate = "req  uesteddate";
            e.GetType();
    try(e!=null)
    {
         XmlSerializer serializer = new XmlSerializer(e.GetType());
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.