I have XSD file which was base to generate (by xsd.exe tool) C# classes for serialization. DotNetCore 3.1. Below is generated code
public partial class Deklaracje {
public PozycjeSzczegolowe PozycjeSzczegolowe {get;set;}
}
public partial class PozycjeSzczegolowe {
[System.Xml.Serialization.XmlElementAttibute("Bank", typeof(BankPozycjeSzczegolowe))]
[System.Xml.Serialization.XmlElementAttibute("PodmiotNZ", typeof(PozycjeSzczegolowePodmiotNZ))]
public object[] Items {get;set;}
}
there is my serialization code to get xml
var myObject = ??? // <= HERE IS PROBLEM
var data = new Declaracje
{
PozycjeSzczegolowe = new PozycjeSzczegolowe { Items = new [] {myObject} }
}
using (var ms = new MemoryStream())
{
using (var writer = new XmlTextWriter(ms, Encoding.UTF8))
{
var xml = new XmlSerializer(typeof(Deklaracje));
xml.Serializer(xmlTextWriter, data)
}
}
My problem is how to pass to myObject both type: Bank and PodmiotNZ objects to success XML generation
- when I try do this by anonymous type I get "Anonymous type cannot be serialized because don't have parameterless constructor"
- when I try use named class "MyClass" I get error "My class is not expected. Use XmlInclude", I tried to add this parameter by parital class to Declaration and PozycjeSzczegolowe classes. Alse I try to pass as extraType param to XmlSerializer constructor, but I still get this error
- Do you have other idea?