Issues with XML Deserialization of List<CustomObject>

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Stubbie
    New Member
    • Apr 2008
    • 7

    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 IXmlSerializabl e interface.

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


    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:


    Code:
    <?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:


    Code:
    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:
    InvalidOperatio nException: {"There is an error in XML document (2, 2)."}
    {"<ArrayOfInput File 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 InvalidOperatio nException: {"There is an error in XML document (2, 2)."} (2,2 cooresponds to the A in ArrayOfInputFil e): {"<ArrayOfInput File xmlns=''> was not expected."}


    Any ideas where I went wrong?

    Thanks
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    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?

    Comment

    • Stubbie
      New Member
      • Apr 2008
      • 7

      #3
      You'll notice in my deserialization I used Inputs.InputFil e instead of List<Inputs.Inp utFile>. That simple change makes everything work!

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

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

        Comment

        Working...