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:
- XmlSerializer s = new XmlSerializer(typeof(List<Inputs.InputFile>));
-
TextWriter w = new StreamWriter("c:\\out.xml");
-
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
-
ns.Add("", "");
-
w.Serialize(w, Global.Pool.InputFiles, ns);
-
w.Close();
This produces the following XML:
- <?xml version="1.0" encoding="utf-8"?>
-
<ArrayOfInputFile>
-
<InputFile FileName="file1.hdr" GUID="XYZ" TimeShift="0">
-
//objects from a list in the InputFile list
-
</InputFile>
-
<InputFile FileName="file2.hdr" GUID="ABC" TimeShift="0">
-
//objects from a list in the InputFile list
-
</InputFile>
-
</ArrayOfInputFile>
This looks well formed and good to me.
I use the following to deserialize:
- XmlSerializer s = new XmlSerializer(typeof(Inputs.InputFile));
-
TextReader r = new StreamReader("c:\\out.xml");
-
List<Inputs.InputFile> a = (List<Inputs.InputFile>)s.Deserialize(r);
-
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