Connecting Tech Pros Worldwide Forums | Help | Site Map

Issues with XML Deserialization of List<CustomObject>

Newbie
 
Join Date: Apr 2008
Posts: 7
#1: Oct 2 '09
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?

Thanks

Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 233
#2: Oct 2 '09

re: Issues with XML Deserialization of List<CustomObject>


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?
Newbie
 
Join Date: Apr 2008
Posts: 7
#3: Oct 2 '09

re: Issues with XML Deserialization of List<CustomObject>


You'll notice in my deserialization I used Inputs.InputFile instead of List<Inputs.InputFile>. That simple change makes everything work!
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 233
#4: Oct 2 '09

re: Issues with XML Deserialization of List<CustomObject>


Ahhh in the typeof! I missed it too :D Glad you got it working!
Reply