470,566 Members | 1,411 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,566 developers. It's quick & easy.

Issues with XML Deserialization of List<CustomObject>

have an object (InputFile) that I am able to successfully serialize to XML and deserialize back into the object through an IXmlSerializable interface.

Now I'm trying to serialize a List<InputFile> to XML. I'm using the following code:

Expand|Select|Wrap|Line Numbers
  1. XmlSerializer s = new XmlSerializer(typeof(List<Inputs.InputFile>));
  2. TextWriter w = new StreamWriter("c:\\out.xml");
  3. XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
  4. ns.Add("", "");
  5. w.Serialize(w, Global.Pool.InputFiles, ns);
  6. w.Close();
This produces the following XML:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <ArrayOfInputFile>
  3.   <InputFile FileName="file1.hdr" GUID="XYZ" TimeShift="0">
  4.   //objects from a list in the InputFile list
  5.   </InputFile>
  6.   <InputFile FileName="file2.hdr" GUID="ABC" TimeShift="0">
  7.   //objects from a list in the InputFile list
  8.   </InputFile>
  9. </ArrayOfInputFile>
This looks well formed and good to me.

I use the following to deserialize:

Expand|Select|Wrap|Line Numbers
  1. XmlSerializer s = new XmlSerializer(typeof(Inputs.InputFile));
  2. TextReader r = new StreamReader("c:\\out.xml");
  3. List<Inputs.InputFile> a = (List<Inputs.InputFile>)s.Deserialize(r);
  4. r.Close();
I get an exception on the Deserialize line:
InvalidOperationException: {"There is an error in XML document (2, 2)."}
{"<ArrayOfInputFile xmlns=''> was not expected."}

I've had no issues Serializing/Deserializing List<>s before.

I've tried variations on providing a fake-namespace or removing the namespace portion altogether. Either way I get an InvalidOperationException: {"There is an error in XML document (2, 2)."} (2,2 cooresponds to the A in ArrayOfInputFile): {"<ArrayOfInputFile xmlns=''> was not expected."}

Any ideas where I went wrong?

Oct 2 '09 #1
3 8174
1,501 Expert 1GB
I'm... at a loss! I've done this fairly recently and it went off without a hitch. The only issue I ran into was the code putting a namespace in, which I solved with the exact same approach you used (I wonder if we found the same article :D).

I can't help but wonder why you're getting that error though... clearly your XML has no namespace in it, so I'm not sure why it's saying it does in the exception. Maybe verify 100% that you're looking at the file you think you are. I've done that a few times where I mix things up and realize I'm looking at the wrong file, especially when I switch between build modes in Visual Studio.

Another random thought... is there anything about namespaces in your class code (those items in square brackets) where you define your XML elements/attributes?
Oct 2 '09 #2
You'll notice in my deserialization I used Inputs.InputFile instead of List<Inputs.InputFile>. That simple change makes everything work!
Oct 2 '09 #3
1,501 Expert 1GB
Ahhh in the typeof! I missed it too :D Glad you got it working!
Oct 2 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by dave v | last post: by
reply views Thread by Christopher Pragash | last post: by
reply views Thread by pak76 | last post: by
2 posts views Thread by Snowman | last post: by
4 posts views Thread by Mike Sarbu | last post: by
3 posts views Thread by parrot toes | last post: by
6 posts views Thread by Bradley Plett | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.