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.