0

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

  1. when I try do this by anonymous type I get "Anonymous type cannot be serialized because don't have parameterless constructor"
  2. 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
  3. Do you have other idea?

1 Answer 1

1

Try following :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace ConsoleApplication181
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;

            Deklaracje data = new Deklaracje()
            {
                PozycjeSzczegolowe = new PozycjeSzczegolowe[]
                {
                    new BankPozycjeSzczegolowe() { name = "John"},
                    new PozycjeSzczegolowePodmiotNZ() { name = "Mary"}
                }
            };

            using (var ms = new MemoryStream())
            {
                using (XmlWriter writer = XmlWriter.Create(ms,settings))
                {
                    XmlSerializer xml = new XmlSerializer(typeof(Deklaracje));
                    xml.Serialize(writer, data);
                }
            }
        }
    }
    public partial class Deklaracje {
       public PozycjeSzczegolowe[] PozycjeSzczegolowe {get;set;}
    }

    [XmlInclude(typeof(BankPozycjeSzczegolowe))]
    [XmlInclude(typeof(PozycjeSzczegolowePodmiotNZ))]
    public partial class PozycjeSzczegolowe {
    }
    public class BankPozycjeSzczegolowe : PozycjeSzczegolowe
    {
        public string name { get; set; }
    }
    public class PozycjeSzczegolowePodmiotNZ : PozycjeSzczegolowe
    {
        public string name { get; set; }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.