For example I have three classes
public class A
{
public string Abc { get; set; }
}
public class B
{
public string Xyz { get; set; }
}
public class C
{
private object itemField;
[XmlElement("A", typeof(A))]
[XmlElement("B", typeof(B))]
public object Item
{
get
{
return itemField;
}
set
{
itemField = value;
}
}
}
And I'm trying to serialize an instance of class C
var b = new B
{
Xyz = "123123"
};
var c = new C
{
Item = b
};
var serializer = new XmlSerializer<C>();
var aaa = serializer.Serialize(c);
Then the output is
-C
--A
---Xyz
----123123
---/Xyz
--/A
-/C
But I'm expecting
-C
--B
---Xyz
----123123
---/Xyz
--/B
-/C
How can I do this? (I converted amazon mws xsd's to classes with xsd.exe, and some output classes are like C class, so I'm having trouble while trying to serialize these classes.)
I'm using net framework 4.6.1 and for serialization XSerializer(nuget.org/packages/XSerializer/0.4.2).
*** EDIT: I found the problem, the problem is not the serializer. "xsd.exe" made mistakes on multidimensional arrays while converting xsd files. I edited the classes for serialization attributes and it worked. Example:
// I changed "[XmlArrayItem("Name", typeof(TypeName))]" To that:
[XmlArrayItem("Name", typeof(TypeName[]))]
public TypeName[][] PropName { get; set; }
Thanks for everyone