0

I have created a project in .net CF 3.5 another project creates an XML files which I will be using in CF project which is in Winform.I am using VS2012. While deserializing the XML I am getting invalid operation exception. The class which does the deserialization is a simple one

class DeserOp
    {
        byte[] contentBytes = Properties.Resources.test; //The test is xml file I put in resources for simplicity         
        XmlSerializer serializer = new XmlSerializer(typeof(Vision));
        MemoryStream stream = new MemoryStream(contentBytes);
        Vision myClass = (Vision)serializer.Deserialize(stream);
    }

Class Vision will be :

public class Vision
    {
        [XmlArrayItem(Type = typeof(Mercedes)),
             XmlArrayItem(Type = typeof(BMW))]
        public List<Cars> ItemArray { get; set; }
        public Vision() { ItemArray = new List<Cars>(); }
    }

Class Cars will be:

public class Cars
    {
        string _name;
        public string Name
        {
            get { return _name; }
            set
            {
                if (_name != value)
                {
                    _name = value;
                }
            }
        }

        public string _type;
        public string Type
        {
            get { return _type; }
            set
            {
                if (_type != value)
                {
                    _type = value;
                }
            }
        }
    }

Class Mercedes/BMW will be:

public class Mercedes : Cars
    {
        string _make;
        public string Make
        {
            get { return _make; }
            set
            {
                if (_make != value)
                {
                    _make = value;
                }
            }
        }
    }

The XML test will be:

The XML will be look alike:

   <?xml version="1.0" encoding="utf-8"?>
    <Vision xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <ItemArray>
        <Mercedes>
          <Name>ABC</Name>
          <Type>Luxery</Type>
          <Make>Cclass</Make>
        </Mercedes>
        <BMW>
          <Name>BDD</Name>
          <Type>Sedan</Type>
          <Make>Perfect</Make>
        </BMW>
      </ItemArray>
    </Vision>

I tried all possible attributes also like [XmlIncludeAttribute(typeof(Mercedes))] on a class Car but still it is throwing error. Also all the elements are public and the file also has public access.

  • Have you tried serializing a populated instance of the Vision class to see what the XML output is and seeing if it matches what you expect? – tcarvin Aug 22 '17 at 12:20
  • Yes it shows the item array as empty structure. – SaurabhUp Aug 23 '17 at 08:23
  • 1
    To be more clear, can you populate your Vision class with a few cars / cars subclasses and *then* serialize it and post that XML to your question for reference and comparison. – tcarvin Aug 23 '17 at 12:39

0 Answers0