0

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

  • Type of is used when the class is inherited by another class. So you need 1) public class A : C 2) public class B : C – jdweng Jan 10 '20 at 13:41
  • What is `XmlSerializer`? Please see [mcve]. – Sinatr Jan 10 '20 at 13:52
  • I'm already used the [XmlElement("",typeof())] on the "public object Item" property. I need to serialize "Item" property by it's type name. (If type of "Item" is A then A tags when type B then B tags etc. not Item tags) – Efekan Tuzcuoğlu Jan 10 '20 at 13:53
  • @Sinatr sorry, my fault. I'm using net framework 4.6.1 and for serialization XSerializer(https://www.nuget.org/packages/XSerializer/0.4.2) – Efekan Tuzcuoğlu Jan 10 '20 at 13:56
  • Then this may be a bug with [`XSerializer`](https://www.nuget.org/packages/XSerializer/). If I serialize your class with the `XmlSerializer` built into .Net then I get the desired results, see https://dotnetfiddle.net/rHGcux. – dbc Jan 11 '20 at 15:48

2 Answers2

0

I don't know about that nuget, but serializing without nugets (I've used extension method from this post):

var test = SerializeObject<C>(c);

produces expected result

<?xml version="1.0" encoding="utf-16"?>
<C xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <B>
    <Xyz>123123</Xyz>
  </B>
</C>
Sinatr
  • 20,892
  • 15
  • 90
  • 319
0

I am getting the desired results with XmlSerializer. Namespace System.Xml.Serialization

var writer = new XmlSerializer(typeof(C));
var file = File.Create(strfilepath);
writer.Serialize(file, c);
file.Close();

Output

<?xml version="1.0"?>
<C xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <B>
    <Xyz>123123</Xyz>
  </B>
</C>
Krishna Varma
  • 4,238
  • 2
  • 10
  • 25